Grpc 令牌过期处理

Grpc token expiration handling

我正在为我的 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;
        }

如何正确解决这个问题?

解决方案:

 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中我们可以检查令牌并更新它。