ahmedsalah
Active member
- Joined
- Sep 26, 2018
- Messages
- 32
- Programming Experience
- 3-5
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
what i have tried
on configure of startup.cs
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
what i have tried
C#:
public async Task InvokeAsync(HttpContext context, DataContext dataContext)
{
var validKey = false;
// than you logic to validate token
var CheckExistAccessToken = context.Request.Headers.ContainsKey("Authorization");
var AccessTokenValue = context.Request.Headers["Authorization"].SingleOrDefault();
//var token = AccessTokenValue.Substring(AccessTokenValue.IndexOf(' ') + 1);
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");
}
//if valid than next middleware Invoke
else
{
await _next.Invoke(context);
// not return to me action i write on postman and return also message not valid token
}
}
}
public static class TokenExtensions
{
public static IApplicationBuilder UseTokenAuth(this IApplicationBuilder builder)
{
return builder.UseMiddleware<TokenValidateMiddleware>();
}
}
C#:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see Enforce HTTPS in ASP.NET Core.
app.UseHsts();
}
app.UseTokenAuth();
app.UseHttpsRedirection();
app.UseStatusCodePagesWithReExecute("/error/{0}");
app.UseMvc();
app.UseCors("CorsData");
app.UseStaticFiles();
app.UseDefaultFiles();
Last edited by a moderator: