C
C#3mo ago
Meteor

JWT handler not working.

I am trying to get a login with a JWT authentication working. I have followed this video https://www.youtube.com/watch?v=GTXvMu6Ex-o&t=1215s . The error I am getting is: JavaScript interop calls cannot be issued at this time. This is because the component is being statically rendered. When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method. The problem came when I tried to add the AuthenticationHandler. This is a part of the handler:
c#
public class AuthenticationHandler : DelegatingHandler
{
private readonly IAuthenticationServices _authenticationServices;
private readonly IConfiguration _configuration;
private bool _refreshing;

public AuthenticationHandler(IAuthenticationServices authenticationServices, IConfiguration configuration)
{
_authenticationServices = authenticationServices;
_configuration = configuration;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var jwt = await _authenticationServices.GetJwtAsync();
var isToServer = request.RequestUri?.AbsoluteUri.StartsWith(_configuration["ServerUrl"] ?? "") ?? false;

if (isToServer && !string.IsNullOrEmpty(jwt))
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
c#
public class AuthenticationHandler : DelegatingHandler
{
private readonly IAuthenticationServices _authenticationServices;
private readonly IConfiguration _configuration;
private bool _refreshing;

public AuthenticationHandler(IAuthenticationServices authenticationServices, IConfiguration configuration)
{
_authenticationServices = authenticationServices;
_configuration = configuration;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var jwt = await _authenticationServices.GetJwtAsync();
var isToServer = request.RequestUri?.AbsoluteUri.StartsWith(_configuration["ServerUrl"] ?? "") ?? false;

if (isToServer && !string.IsNullOrEmpty(jwt))
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
This is the GetJwtAsync method.
c#
public async ValueTask<string> GetJwtAsync()
{
if (string.IsNullOrEmpty(_jwtCache))
_jwtCache = await _sessionStorageService.GetItemAsync<string>(JWT_KEY);

return _jwtCache;
}
c#
public async ValueTask<string> GetJwtAsync()
{
if (string.IsNullOrEmpty(_jwtCache))
_jwtCache = await _sessionStorageService.GetItemAsync<string>(JWT_KEY);

return _jwtCache;
}
Which gets called by the handler on this line var jwt = await _authenticationServices.GetJwtAsync(); when debugging, the error gets after this line var jwt = await _authenticationServices.GetJwtAsync();
Coding Tutorials
YouTube
Blazor Authentication with JSON Web Tokens
We've seen a ASP.NET backend with an Angular frontend. What if we want C# all the way through? Here's how we do it in Blazor. Source code available at: https://github.com/JasperKent/Blazor-Wasm-Authentication Server code available at: https://github.com/JasperKent/WebApi-Authentication Topics include: - Creating HttpClient through an HttpClien...
No description
No description
No description
No description
0 Replies
No replies yetBe the first to reply to this messageJoin