I am using Asp.net Webform project with Web API. I configured JWT token-based authentication and now I want to customize the authentication response
Here are my configurations,
Startup.cs
public class Startup { public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); // Web API routes config.MapHttpAttributeRoutes(); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); ConfigureOAuth(app); app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); WebApiConfig.Register(config); } public void ConfigureOAuth(IAppBuilder app) { String apiHttpOnly = ConfigurationManager.AppSettings["AllowInsecureHttp"]; String tokenTimeSpan = ConfigurationManager.AppSettings["tokenTimeSpanFromMinutes"]; bool allowInsecureHttp = !String.IsNullOrEmpty(apiHttpOnly) ? Convert.ToBoolean(apiHttpOnly) : false; int accessTokenExpireTimeSpan = !String.IsNullOrEmpty(tokenTimeSpan) ? Convert.ToInt32(tokenTimeSpan) : 60; var authProvider = new AuthorizationServiceProvider(); OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions { //For Dev enviroment only (on production should be AllowInsecureHttp = false) AllowInsecureHttp = allowInsecureHttp, TokenEndpointPath = new PathString("/api/authenticate"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(accessTokenExpireTimeSpan), Provider = authProvider }; app.UseOAuthAuthorizationServer(options); } }
AuthorizationServiceProvider
public class AuthorizationServiceProvider : OAuthAuthorizationServerProvider { public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { context.Validated(); return base.ValidateClientAuthentication(context); } public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var identity = new ClaimsIdentity(context.Options.AuthenticationType); if (Membership.ValidateUser(context.UserName, context.Password)) { identity.AddClaim(new Claim(ClaimTypes.Role, "admin")); identity.AddClaim(new Claim("username", context.UserName)); identity.AddClaim(new Claim(ClaimTypes.Name, "admin admin")); context.Validated(identity); } else { context.SetError("invalid_grant", "Provide username and password is incorrect."); } return base.GrantResourceOwnerCredentials(context); } }
When I call the API with the right credentials, it returns like
{
"access_token": "uEwmXl6N0mJXVUZesxA_2tG5lIuZUIUDaxtjAl0QGE6j2-J7n4c63zboOUClGjRQf1IDY9-nBgyq0HP5WR7MMxTYoHGIyiHIbcKu9AYwhECCGaVBCxY2Ounhit4N1pYK1vV6uX6AcoA-a0xhytF8Jz27D77ZvCLi3PuUQDEXSp0pkGG796wu1fRZCaRsCB-kLoa-_V7KJaGGhhoybN_c0GNOBhhwmGpx6Js26-Vx-lmWpfsPUE1aKrJfx-oMcyE5x7CooAlx4vA6iZhnNfmYdRejRKoKKnObyuAsym7mVdZY3bpv",
"token_type": "bearer",
"expires_in": 5183999
}
I want to customize the response by adding some extra attributes like,
{
"access_token": "uEwmXl6N0mJXVUZesxA_2tG5lIuZUIUDaxtjAl0QGE6j2-J7n4c63zboOUClGjRQf1IDY9-nBgyq0HP5WR7MMxTYoHGIyiHIbcKu9AYwhECCGaVBCxY2Ounhit4N1pYK1vV6uX6AcoA-a0xhytF8Jz27D77ZvCLi3PuUQDEXSp0pkGG796wu1fRZCaRsCB-kLoa-_V7KJaGGhhoybN_c0GNOBhhwmGpx6Js26-Vx-lmWpfsPUE1aKrJfx-oMcyE5x7CooAlx4vA6iZhnNfmYdRejRKoKKnObyuAsym7mVdZY3bpv",
"token_type": "bearer",
"expires_in": 5183999,
"attribute1" : "abc",
"attribute2" : "ert"
}
Anyone have an idea to do that?