Overview
This article explains how to use OWIN to implement the validation capabilities of the ASP. NET Web API, and the mechanism to avoid repeating the user name and password during client-server interaction.
Clients can be divided into two categories:
- JavaScript: Can be understood as Web pages
- Native: Including mobile app, Windows client, etc.
Steps
- For access tokens via username and password, please refer to:
http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/
- To implement the refresh token mechanism, please refer to:
http://bitoftech.net/2014/07/16/enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin/
Sample code
Please refer to: https://github.com/attilah/AngularJSAuthentication
Verification process
- The user accesses the API for the first time, submitting the user name, password, and client ID (that is, the application ID that describes which application is coming from).
- The server returns access tokens (short-term, such as half-hour) and refresh token (valid for a longer period, such as six months).
- Within half an hour, users can interact with the server using Access tokens, without having to submit a user name and password.
- After half an hour, access token expires and the user submits a refresh token and client ID to obtain a new access token.
- Each time the access token is refreshed, the original refresh token is deleted, and a new refresh token is generated and the validity period is postponed.
- If the user does not have access to the server during the refresh token validity period, the refresh token expires and the user name and password will be submitted on the next visit.
Understanding the principle is far more important than reading the sample code, and according to the above procedure, you can customize the code to achieve more detailed control.
Ext.: http://www.cnblogs.com/csharpstyle/articles/4928019.html
(GO) "ASP. Authentication with OWIN"