Authentication means verifying the user who is accessing the system. We have available different types of authentication in .NET programming like Windows Authentication, Forms Authentication, Claim Based Authentication, Token-Based Authentication, etc.
Token-based Authentication
In token-based authentication, you pass your credentials [user name and password], to the server, which verifies your credentials and if it is a valid user, then it will return a signed token to the client system, which has an expiration time. The client can store this token locally using any mechanism like local storage, session storage, etc. and if the client makes any other call to the server for data, then it does not need to pass its credentials every time. The client can directly pass the token to the server, which will be validated by the server and if the token is valid, then you will able to access your data.
OAuth
(Open Authorization) is an open standard for token-based authentication and authorization on the Internet.
OAuth versions
There are two versions of OAuth authorization OAuth 1 (using HMAC-SHA signature strings) and OAuth 2 (using tokens over HTTPS).
OAuth Tokens
There are two types of tokens involved in OAuth 2,
Access Token
The access token is used to for authentication and authorization to get access to the resources from the resource server.
Refresh Token
The refresh token normally is sent together with the access token.
The refresh token is used to get a new access token when the old one expires. Instead of the normal grant type, the client provides the refresh token and receives a new access token.
Token Types
Access tokens have a type, which defines how they are constructed.
- Bearer Tokens
The bearer tokens use HTTPS security, and the request is not signed or encrypted. Possession of the bearer token is considered authentication. - MAC Tokens
More secure than bearer tokens, MAC tokens are similar to signatures, in that they provide a way to have (partial) cryptographic verification of the request.
How To Implement OAuth in WebAPI
Step 1 - Create a WebAPI Project
Step 2 - Add the below packages from Nuget Packages
Packages Required
- Microsoft.Owin – Implementation of OAuth Services.
- Microsoft.Owin.Host.SystemWeb- OWIN server that enables OWIN-based applications to run on IIS using the ASP.NET request pipeline.
- Microsoft.Owin.Security.OAuth- Middleware that enables an application to support any standard OAuth 2.0 authentication workflow.
- Microsoft.Owin.Security- Common types which are shared by the various authentication middleware components.
- Microsoft.AspNet.Identity.Owin-Owin implementation for ASP.NET Identity.
- Microsoft.Owin.Cors - components to enable Cross-Origin Resource Sharing (CORS) in OWIN middleware.
Step 3 - Owin Startup Configuration
Remove the Global.asax.cs file and add a startup file in place of that. Add the below line for creating owin start-up.
[assembly: OwinStartup(typeof(EmployeeService.Startup))]
If Application-specific functions like Application_Init, Application_Start, etc are needed we can make Global.asax itself as owin startup.
In a startup, the file adds the below configurations of Token. This will act as Web API Configuration and when every time web API is refreshed this configuration will also be refreshed.
- IAppBuilder AppBuilder;
- public void ConfigureAuth(IAppBuilder app) {
- AppBuilder = app;
- var OAuthOptions = new OAuthAuthorizationServerOptions {
- AllowInsecureHttp = true,
- TokenEndpointPath = new PathString("/token"),
- AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(1),
- Provider = new SimpleAuthorizationServerProvider(),
- RefreshTokenProvider = new SimpleRefreshTokenProvider()
- };
- app.UseOAuthBearerTokens(OAuthOptions);
- app.UseOAuthAuthorizationServer(OAuthOptions);
- app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
- HttpConfiguration config = new HttpConfiguration();
- WebApiConfig.Register(config);
- }
- public void Configuration(IAppBuilder app) {
- ConfigureAuth(app);
- GlobalConfiguration.Configure(WebApiConfig.Register);
- }
- TokenEndpointPath - It’s the request to be sent from the client end to generate the token.
- AccessTokenExpireTimeSpan - This is the access token expiry time.
- Provider/ RefreshTokenProvider - It’s a class where the token generation functionalities are included.
Step 4 - Adding Token & Refresh Token Providers
Token Provider
"SimpleAuthorizationServerProvider" class which inherits from OAuthAuthorizationServerProvider. It contains two override methods.
- ValidateClientAuthentication – This method is to validate each service request.
- c– This method will grant to access the application based on your credentials.
- GrantRefreshToken – This method is used to generate the refresh token.

Lijish BhavanPosted Apr 28, 2024, 9:09 AM
Is this code available ? could you please provide the code for this project.
patrick nolanPosted Jan 21, 2022, 5:46 PM
Is there a link to the full code? Trying to follow your snippets but without context I'm not sure exactly how to place them.