Open phields opened 4 months ago
Hi, could you provide a sample project or share your authentication filter, along with how you're verifying your tokens? I don't think your issue is related to MagicOnion. I'm using LitJWT (you can find it here: https://github.com/Cysharp/LitJWT) and have successfully verified my tokens each time.
You can find my implementation in the following repository: https://github.com/licentia88/MagicOnionGenericTemplate
I'm very sorry, but it seems that there is a bug in the sample code. If you fix WithAuthenticationFilter.SendAsync
as follows, it will work.
public async ValueTask<ResponseContext> SendAsync(RequestContext context, Func<RequestContext, ValueTask<ResponseContext>> next)
{
if (AuthenticationTokenStorage.Current.IsExpired)
{
Console.WriteLine($@"[WithAuthenticationFilter/IAccountService.SignInAsync] Try signing in as '{_signInId}'... ({(AuthenticationTokenStorage.Current.Token == null ? "FirstTime" : "RefreshToken")})");
var client = MagicOnionClient.Create<IAccountService>(_channel);
var authResult = await client.SignInAsync(_signInId, _password);
if (!authResult.Success)
{
throw new Exception("Failed to sign-in on the server.");
}
Console.WriteLine($@"[WithAuthenticationFilter/IAccountService.SignInAsync] User authenticated as {authResult.Name} (UserId:{authResult.UserId})");
AuthenticationTokenStorage.Current.Update(authResult.Token, authResult.Expiration); // NOTE: You can also read the token expiration date from JWT.
if (context.CallOptions.Headers?.FirstOrDefault(x => string.Equals(x.Key, "Authorization", StringComparison.OrdinalIgnoreCase)) is {} entry)
{
context.CallOptions.Headers?.Remove(entry);
}
}
if (!context.CallOptions.Headers?.Any(x => string.Equals(x.Key, "Authorization", StringComparison.OrdinalIgnoreCase)) ?? false)
{
context.CallOptions.Headers?.Add("Authorization", "Bearer " + AuthenticationTokenStorage.Current.Token);
}
return await next(context);
}
Hello,
I've encountered an issue with JWT authentication in the gRPC client demo. The problem occurs when making multiple requests using the same
IGreeterService
client. Here's a detailed description of the issue:greeterClient.HelloAsync()
succeeds and authenticates correctly.greeterClient.HelloAsync()
fail with a 401 Unauthorized error.MagicOnionClient
instance before eachHelloAsync()
call.Here's a simplified version of the code that demonstrates the issue:
Could you please advise on what might be causing this behavior and how to correctly handle multiple authenticated requests using the same client instance?