ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.3k

How to use middle war to get next request asp.net core 2

Sep 15 2019 2:51 PM
How to get next request pipline in case of token success .
suppose i need to get data from action result get request by post man
  1. [HttpGet(Contracts.ApiRoutes.Security.GetUserMenus)]  
  2.        public IActionResult GetUserMenu(string userId)  
  3.        {  
  4.            string strUserMenus = _SecurityService.GetUserMenus(userId);  
  5.            return Ok(strUserMenus);  
  6.        }  
 
i write on postman link as  then select get then write on header 
 
key : authorization  
value :eeerrttyyyyy
 
when invalid access token then it will show message invalid token
 
if valid access token it will continue execution and get result from action 
 
this is actually i need on next but cannot do it
  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.   
  9.   
  10.   
  11.  if (CheckExistAccessToken) {  
  12.   
  13.   bool isvalid = _tockenvalidator.ValidateToken(AccessTokenValue);  
  14.   if (isvalid) {  
  15.    validKey = true;  
  16.   } else {  
  17.    validKey = false;  
  18.   }  
  19.   
  20.   
  21.  }  
  22.  if (!validKey) {  
  23.   context.Response.StatusCode = (int) HttpStatusCode.Forbidden;  
  24.   await context.Response.WriteAsync("Invalid Token");  
  25.  }  
  26.  //if validm than next middleware Invoke    
  27.  else {  
  28.   //success token  
  29.   context.Request.EnableRewind();  
  30.   await _next.Invoke(context);  
  31.   // how to get next request meaning i need to get result of action getusermenu    
  32.   
  33.  }  
  34.   
  35.   
  36. }  
  37. }
 my result show on post man as below but on my app show invalid token although
on i make debug and token is valid and reach until
await _next.Invoke(context); but not show result
actually i need to show data display on postman