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 .
- public async Task InvokeAsync(HttpContext context, DataContext dataContext)
- {
- var validKey = false;
-
-
- var CheckExistAccessToken = context.Request.Headers.ContainsKey("Authorization");
- var AccessTokenValue = context.Request.Headers["Authorization"].SingleOrDefault();
-
-
-
- if (CheckExistAccessToken)
- {
-
- bool isvalid = _tockenvalidator.ValidateToken(AccessTokenValue);
- if (isvalid)
- {
- validKey = true;
- }
- else
- {
- validKey = false;
- }
-
-
- }
- if (!validKey)
- {
- context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
- await context.Response.WriteAsync("Invalid Token");
- }
-
- else
- {
- await _next.Invoke(context);
-
-
- }
- }
- }
- public static class TokenExtensions
- {
- public static IApplicationBuilder UseTokenAuth(this IApplicationBuilder builder)
- {
- return builder.UseMiddleware();
-
- }
- }
- on configure of startup.cs
-
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
-
- app.UseHsts();
- }
-
-
- app.UseTokenAuth();
-
- app.UseHttpsRedirection();
-
- app.UseStatusCodePagesWithReExecute("/error/{0}");
-
- app.UseMvc();
- app.UseCors("CorsData");