I have an application with classic client server architecture. ASP.NET Core is in the backend and in the frontend jQuery, Angular or vanilla-js.
For authentication I use an OpenID-Connect certified server. (keycloak)
So far I can login via the asp.net core Authentication Middleware. Get my Access Token and Refresh Token.
My configuration looks like this:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.RequireHttpsMetadata = false;
options.MetadataAddress = Configuration["IdentityServer:Metadata"];
options.ClientId = Configuration["IdentityServer:ClientId"];
options.ClientSecret = Configuration["IdentityServer:ClientSecret"];
options.SaveTokens = true;
options.ResponseType = "code";
options.Scope.Add("openid profile email web origons roles SocpoeOnlyForDeveopers");
});
Now I want to access resources with the received Access Token, not only from my backend but also from my frontend.
So far I have only found one solution: to store the access token in a js variable in the _Layout.cshtml.
When the access token expires I make a request to my backend to get a new access token. In the backend I then ask the authorization server with the refresh token to get a new access token. Then I send the access token back to the frontend.
A bit like here: https://medium.com/@ognjanovski.gavril/use-refresh-token-to-renew-access-token-and-resend-all-unauthorized-401-requests-that-failed-190e9c97fc3a
Because my access tokens expire after 5 min (to short?) (default of keycloak) there would be quite a lot network activity.
Is there a better way?