ASP.NET Core with JWT Authentication Demo
- Compatibile for
Visual Studio 2017
andVisual Studio for Mac
- Base on
.NET Core 2.0
Swashbuckle.AspNetCore
for Swagger API documentationDapper
for ORMMailKit
for email sendingAzure SQL Database
(SQL Server standalone also fine)
- Access Token: JWT token genrate by
POST: /api/tokens/access
the Sign In API, can be refreshed, the access token has not been stored - Refresh Token: JWT token genrate by
POST: /api/tokens/access
the Sign In API, will be replaced by new one if call refresh API, the refresh token has not been stored - Active Token: JWT token genrate by
POST: /api/users
the Create Account API andGET: /api/users/sendactiveemail/{email}
the Send Account Activation Mail API, use for active account, it has been stored in Token table till finish account activation - Rest Password Token: JWT token genrate by
GET: /api/users/sendresetmail/{email}
the Send Reset Password Mail API, use for reset account password, it has been stored in Token table till finish password update.
- Create your database manually and run the user and token tables create script under
netcoreauth.model
projectScripts
folder. - Update database connection string in
appsettings.json
- If you want to use mail sending for account activation, please update the
Mail.cs
class file innetcoreauth.model
project, suggest to use Gmail, I tested by Gmail successful in my project. - After all, try
http://[localhost]:[port]/swagger
, to get API document GET: /api/tests/1
this API without Auth,GET: /api/tests
this API with Auth- Call
POST: /api/users
to create account - Get the token in you database token table, then call
PUT: /api/users/active/{token}
to active account - Call
POST: /api/tokens/access
to use your email and password login and get tokens (access token and refresh token)POST /api/tokens/access { "email": "[email protected]", "password": "xxxxxxxxxxxxxxxxxxxxxxx" }
- Call
GET: /api/tests
with header:Authorization: Bearer {token}
, both access token and refresh token work fineGET /api/tests Authorization: Bearer xxxxxxxxxxx
- Get response:
["value1", "value2"]
without401
HTTP code fromGET: /api/tests
, that means you get success.
Database script addMore detail usage description for this demo- Send mail async implementation
- Add Postman script samples for API calls
Upgrade to .NET Core 2.0- Add API version support