GithubHelp home page GithubHelp logo

netcore2-jwt's Introduction

netcore2-jwt

This is a .NET Core 2 sample project showing how to use JWTs (JSON Web Tokens). It is the companion code for the article Securing ASP.NET Core 2.0 Applications with JWTs.

The JWT project defines a Web API application whose authentication is based on JWT.

Running the project

The solution contains a Test project with four integration tests validating the application behaviour. You can run the tests from Visual Studio 2017 or by typing dotnet test in a command window.

If you want to interactively test the application, you can use Postman or any other Http client.

  1. Run the project from Visual Studio 2017 or by typing dotnet run in a command window
  2. Launch Postman and make a GET request as follows:
    GET http://localhost:63939/api/books HTTP/1.1
    cache-control: no-cache
    Accept: */*
    Host: localhost:63939
    accept-encoding: gzip, deflate
    Connection: keep-alive

This should return a 401 HTTP status code (Unauthorized)

  1. Make a POST request like the following:
    POST http://localhost:63939/api/token HTTP/1.1
    cache-control: no-cache
    Content-Type: application/json
    Accept: */*
    Host: localhost:63939
    accept-encoding: gzip, deflate
    content-length: 39
    Connection: keep-alive
    
    {username: "mario", password: "secret"}

It returns a JSON object like the following:

    {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNYXJpbyBSb3NzaSIsImVtYWlsIjoibWFyaW8ucm9zc2lAZG9tYWluLmNvbSIsImp0aSI6IjVkNTRkMzIwLWQ3N2EtNDFhMy1iZTcwLTc2M2UyMGRmMjE3MyIsImV4cCI6MTUxMTE3NzQwMywiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo2MzkzOS8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjYzOTM5LyJ9.g0yooTf3DJO43yL8bT4VE_VIdc5WHFhCVb3u9Jg7VTk"
    }
  1. The following GET request
    GET http://localhost:63939/api/books HTTP/1.1
    cache-control: no-cache
    Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNYXJpbyBSb3NzaSIsImVtYWlsIjoibWFyaW8ucm9zc2lAZG9tYWluLmNvbSIsImp0aSI6IjVkNTRkMzIwLWQ3N2EtNDFhMy1iZTcwLTc2M2UyMGRmMjE3MyIsImV4cCI6MTUxMTE3NzQwMywiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo2MzkzOS8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjYzOTM5LyJ9.g0yooTf3DJO43yL8bT4VE_VIdc5WHFhCVb3u9Jg7VTk
    Accept: */*
    Host: localhost:63939
    accept-encoding: gzip, deflate
    Connection: keep-alive

returns the following response:

	[
	    {
	        "author": "Ray Bradbury",
	        "title": "Fahrenheit 451",
			"ageRestriction": false
	    },
	    {
	        "author": "Gabriel García Márquez",
	        "title": "One Hundred years of Solitude",
			"ageRestriction": false
	    },
	    {
	        "author": "George Orwell",
	        "title": "1984",
			"ageRestriction": false
	    },
	    {
	        "author": "Anais Nin",
	        "title": "Delta of Venus",
			"ageRestriction": true
	    }
	]

netcore2-jwt's People

Contributors

andychiare avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.