I have a middle ware class as below and expecting token for allow-anonymous method. Can some suggest me what am i missing over here.
Middleware class as follows :
- namespace filetrs
- {
- public class TestingAuthenticationFilter : IAuthenticationFilter
- {
- public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
- {
- var alias = GetAliasFromJwt(context.Request.Headers.Authorization.ToString());
- await Task.Yield();
- }
- public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
- {
- return Task.CompletedTask;
- }
- public static string GetAliasFromJwt(string jwt)
- {
- string alias, extractedToken;
- alias = extractedToken = string.Empty; string tokenTypeToRemove = "Bearer ";
- if (tokenTypeToRemove.Length > 0)
- {
- extractedToken = jwt.Substring(tokenTypeToRemove.Length);
- var handler = new JwtSecurityTokenHandler();
- var jwtSecurityToken = handler.ReadToken(extractedToken) as JwtSecurityToken;
- alias = jwtSecurityToken.Claims.First(claim => claim.Type == "abc").Value.Split('@').First();
- }
- return alias;
- }
- }
Please find APi class as well
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
-
- config.MapHttpAttributeRoutes();
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional } );
- config.Filters.Add(new TestingAuthenticationFilter());
- config.Filters.Add(new ExceptionHandlingAttribute());
- }}
ConfigAuth method from startup.cs as follows
- public void ConfigureAuth(IAppBuilder app)
- {
- app.UseWindowsAzureActiveDirectoryBearerAuthentication
- ( new WindowsAzureActiveDirectoryBearerAuthenticationOptions
- { Tenant = tenant, TokenValidationParameters = new TokenValidationParameters { ValidAudience = clientId }
- });
I am unable to find the solution. Please someone help me on this?