SECURING WEBAPI USING JSON WEB TOKEN (JWT) IN WEB API C#
ref site : http://www.decatechlabs.com/secure-webapi-using-jwt
Step 1 :
Step 2 :
Step 3 :
Step 4 :
Now replace all code with below code
And now you have to add following name spaces
1 using System.Net.Http;
2 using System.Threading.Tasks;
3 using System.Net;
4 using System.Threading;
And Install System.IdentityModel.Tokens.Jwt from nuget
after install upper package add following name space
1 using Microsoft.IdentityModel.Tokens;
2 using System.IdentityModel.Tokens.Jwt;
Step 5 :
Now add Login Controller and write below code in it.
Step 1 :
Step 2 :
Step 3 :
Step 4 :
Now replace all code with below code
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace WEBAPI_JWT_Authentication
{
internal class TokenValidationHandler : DelegatingHandler
{
private static bool TryRetrieveToken(HttpRequestMessage request, out string token)
{
token = null;
IEnumerable<string> authzHeaders;
if (!request.Headers.TryGetValues("Authorization", out authzHeaders) || authzHeaders.Count() > 1)
{
return false;
}
var bearerToken = authzHeaders.ElementAt(0);
token = bearerToken.StartsWith("Bearer ") ? bearerToken.Substring(7) : bearerToken;
return true;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpStatusCode statusCode;
string token;
//determine whether a jwt exists or not
if (!TryRetrieveToken(request, out token))
{
statusCode = HttpStatusCode.Unauthorized;
//allow requests with no token - whether a action method needs an authentication can be set with the claimsauthorization attribute
return base.SendAsync(request, cancellationToken);
}
try
{
const string sec = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";
var now = DateTime.UtcNow;
var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(sec));
SecurityToken securityToken;
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
TokenValidationParameters validationParameters = new TokenValidationParameters()
{
ValidAudience = "http://localhost:50191",
ValidIssuer = "http://localhost:50191",
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
LifetimeValidator = this.LifetimeValidator,
IssuerSigningKey = securityKey
};
//extract and assign the user of the jwt
Thread.CurrentPrincipal = handler.ValidateToken(token, validationParameters, out securityToken);
HttpContext.Current.User = handler.ValidateToken(token, validationParameters, out securityToken);
return base.SendAsync(request, cancellationToken);
}
catch (SecurityTokenValidationException e)
{
statusCode = HttpStatusCode.Unauthorized;
}
catch (Exception ex)
{
statusCode = HttpStatusCode.InternalServerError;
}
return Task<HttpResponseMessage>.Factory.StartNew(() => new HttpResponseMessage(statusCode){ });
}
public bool LifetimeValidator(DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValidationParameters validationParameters)
{
if (expires != null)
{
if (DateTime.UtcNow < expires) return true;
}
return false;
}
}
}
And now you have to add following name spaces
1 using System.Net.Http;
2 using System.Threading.Tasks;
3 using System.Net;
4 using System.Threading;
And Install System.IdentityModel.Tokens.Jwt from nuget
after install upper package add following name space
1 using Microsoft.IdentityModel.Tokens;
2 using System.IdentityModel.Tokens.Jwt;
Step 5 :
Now add Login Controller and write below code in it.
Step 6 :
Now Add following lines in WebApiConfig.cs
config.MessageHandlers.Add(new TokenValidationHandler());
Step 7 :
Let’s decorate our Values Controller with Authorize attribute and Run the project press F5
Comments
Post a Comment