ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.3k

When Success Valid Access Token Not Return Result on next ?

Sep 13 2019 8:22 PM
I validate token using middle ware in asp.net core 2.2 in case of access token not valid return message not valid and this case work perfect

problem come when valid token success the problem is next request no give me result of action executed so that what i do for that working

problem is when success valid token is OK it reach until next but not display after that action that have result

in both cases if valid token or not valid return invalid token message .
  1. public async Task InvokeAsync(HttpContext context, DataContext dataContext)  
  2.         {  
  3.             var validKey = false;  
  4.   
  5.             // than you logic to validate token                
  6.             var CheckExistAccessToken = context.Request.Headers.ContainsKey("Authorization");  
  7.             var AccessTokenValue = context.Request.Headers["Authorization"].SingleOrDefault();  
  8.             //var token = AccessTokenValue.Substring(AccessTokenValue.IndexOf(' ') + 1);  
  9.   
  10.            
  11.             if (CheckExistAccessToken)  
  12.             {  
  13.                 
  14.                 bool isvalid = _tockenvalidator.ValidateToken(AccessTokenValue);  
  15.                 if (isvalid)  
  16.                 {  
  17.                     validKey = true;  
  18.                 }  
  19.                 else  
  20.                 {  
  21.                     validKey = false;  
  22.                 }  
  23.                  
  24.   
  25.                 }  
  26.             if (!validKey)  
  27.             {  
  28.                 context.Response.StatusCode = (int)HttpStatusCode.Forbidden;  
  29.                 await context.Response.WriteAsync("Invalid Token");  
  30.             }  
  31.             //if valid than next middleware Invoke  
  32.             else  
  33.             {  
  34.                 await _next.Invoke(context);  
  35. // not return to me action i write on postman and return also message not valid token   
  36.                  
  37.             }  
  38.         }  
  39.     }  
  40.  public static class TokenExtensions  
  41.     {  
  42.         public static IApplicationBuilder UseTokenAuth(this IApplicationBuilder builder)  
  43.         {  
  44.               return builder.UseMiddleware();  
  45.               
  46.         }  
  47.     }  
  48. on configure of startup.cs  
  49.   
  50.  if (env.IsDevelopment())  
  51.             {  
  52.                 app.UseDeveloperExceptionPage();  
  53.             }  
  54.             else  
  55.             {  
  56.                 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.  
  57.                 app.UseHsts();  
  58.             }  
  59.              
  60.              
  61.             app.UseTokenAuth();   
  62.   
  63.             app.UseHttpsRedirection();  
  64.              
  65.             app.UseStatusCodePagesWithReExecute("/error/{0}");  
  66.           
  67.             app.UseMvc();  
  68.             app.UseCors("CorsData");  
 

Answers (2)