Grpc令牌过期处理
Posted
技术标签:
【中文标题】Grpc令牌过期处理【英文标题】:Grpc token expiration handling 【发布时间】:2021-08-22 17:37:04 【问题描述】:我在我的 blazor-wasm 应用程序中使用 grpc 代码优先,但我不明白应该如何处理令牌过期。
据我了解,我需要一个客户端拦截器来检查过期时间并向服务器发出更新请求。
public static WebAssemblyHostBuilder AddGrpc(this WebAssemblyHostBuilder builder)
builder.Services
.AddTransient(sp => sp.GetRequiredService<IHttpClientFactory>()
.CreateClient(BaseClient));
builder.Services.AddScoped(services =>
var baseAddressMessageHandler = services.GetRequiredService<AuthenticationHeaderHandler>(); // <= adds the authorization header
baseAddressMessageHandler.InnerHandler = new HttpClientHandler(); // <= I tried adding a custom handler, but it didn't work either
var grpcWebHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, baseAddressMessageHandler);
var channel = GrpcChannel.ForAddress(builder.HostEnvironment.BaseAddress, new GrpcChannelOptions HttpHandler = grpcWebHandler );
return channel;
);
return builder;
如何正确解决这个问题?
【问题讨论】:
【参考方案1】:解决方案:
builder.Services.AddScoped(services =>
var authManager = services.GetRequiredService<IAuthenticationManager>();
var navManager = services.GetRequiredService<NavigationManager>();
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
try
await authManager.TryRefreshToken();
catch (Exception ex)
Console.WriteLine(ex.Message);
navManager.NavigateTo("/login");
);
var baseAddressMessageHandler = services.GetRequiredService<AuthenticationHeaderHandler>();
baseAddressMessageHandler.InnerHandler = new HttpClientHandler();
var grpcWebHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, baseAddressMessageHandler);
var channel = GrpcChannel.ForAddress(builder.HostEnvironment.BaseAddress, new GrpcChannelOptions
HttpHandler = grpcWebHandler,
Credentials = ChannelCredentials.Create(new SslCredentials(), credentials)
);
return channel;
);
在CallCredentials.FromInterceptor
我们可以检查令牌并更新它。
【讨论】:
以上是关于Grpc令牌过期处理的主要内容,如果未能解决你的问题,请参考以下文章