We are using our own service from which with a request we get a bearer token. This bearer token is copied and then paste in all of the API requests. But I have to make a change - when this token is expired, to refresh it.
I am a little bit lost how to do that. I am not supposed to add a new model or controller. Here is the code:
Loading
Vishal YelvePosted Oct 5, 2023, 4:20 AM
Hi,
Do refer below link
https://www.c-sharpcorner.com/article/jwt-authentication-with-refresh-tokens-in-net-6-0/#:~:text=We%20can%20create%20a%20new%20folder%20%E2%80%9CAuth%E2%80%9D%20and%20create%20%E2%80%9C,authentication%20under%20the%20Auth%20folder.&text=In%20the%20above%20class%2C%20we,and%20refresh%20token%20expiry%20time.
Rositsa RusevaPosted Oct 2, 2023, 11:37 AM
public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
var accessToken = await GetAccessTokenAsync(context);
var userRights = await rightsService.GetUserRights(accessToken);
if (userRights == null || !userRights.Contains(rightId))
context.Result = new ForbidResult();
}
private async Task
{
var filteredIdentities = _identitiesConfiguration
.Where(x => x != null)
.Distinct()
.Select((_, i) => $"Bearer{i}")
.ToArray();
var token = string.Empty;
foreach (var filteredIdentity in filteredIdentities)
{
try
{
token = await context.HttpContext.GetTokenAsync(filteredIdentity, "access_token");
if (!string.IsNullOrEmpty(token))
{
break;
}
}
catch (Exception)
{
// Empty
}
}
if (string.IsNullOrEmpty(token))
{
throw new UnauthorizedAccessException("The token for checking the application rights is not set or has expired.");
}
return token;
}