ASP.NET Core with JWT Authentication Demo
- Compatibile for
Visual Studio 2017andVisual Studio for Mac - Base on
.NET Core 2.0 Swashbuckle.AspNetCorefor Swagger API documentationDapperfor ORMMailKitfor email sendingAzure SQL Database(SQL Server standalone also fine)
- Access Token: JWT token genrate by
POST: /api/tokens/accessthe Sign In API, can be refreshed, the access token has not been stored - Refresh Token: JWT token genrate by
POST: /api/tokens/accessthe 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/usersthe 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.modelprojectScriptsfolder. - Update database connection string in
appsettings.json - If you want to use mail sending for account activation, please update the
Mail.csclass file innetcoreauth.modelproject, 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/1this API without Auth,GET: /api/teststhis API with Auth- Call
POST: /api/usersto create account - Get the token in you database token table, then call
PUT: /api/users/active/{token}to active account - Call
POST: /api/tokens/accessto 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/testswith header:Authorization: Bearer {token}, both access token and refresh token work fineGET /api/tests Authorization: Bearer xxxxxxxxxxx - Get response:
["value1", "value2"]without401HTTP 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