C# 在Grpc.net服务中使用AuthenticationHandler

C# 在Grpc.net服务中使用AuthenticationHandler,c#,authentication,grpc,C#,Authentication,Grpc,我尝试在Grpc服务中使用自定义AuthenticationHandler 但似乎我理解错了什么。(我不是Asp方面的专家) 我想中断呼叫并向客户端发出身份验证失败的信号。 这是我想到的 public class HostAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> { protected override async Task<AuthenticateResult&

我尝试在Grpc服务中使用自定义AuthenticationHandler 但似乎我理解错了什么。(我不是Asp方面的专家) 我想中断呼叫并向客户端发出身份验证失败的信号。 这是我想到的

public class HostAuthenticationHandler
 : AuthenticationHandler<AuthenticationSchemeOptions>
{
   protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
   {
    var result = await Task.Run(() => 
    {   
       // first try 
       throw new RpcException(new Status(StatusCode.PermissionDenied, ""));
       
       // second try 
       return AuthenticateResult.Fail("XYZ.");
    });
    return result;
   }
}
公共类HostAuthenticationHandler
:AuthenticationHandler
{
受保护的重写异步任务handleAuthenticateAync()
{
var result=等待任务。运行(()=>
{   
//初试
抛出新的RpcException(新状态(StatusCode.PermissionDenied)(“”);
//第二次尝试
返回AuthenticateResult.Fail(“XYZ”);
});
返回结果;
}
}
我的第一次尝试是抛出一个RpcException,但是状态代码没有显示在客户端中。这里总是出现内部服务器错误(501)。之前更改Response.StatusCode也不会出现在客户端中

我的第二次尝试是AuthenticateResult.Fail结果,但这似乎没有用

MyStartup.cs非常简单

services.AddAuthentication("BasicAuthentication")
    .AddScheme<AuthenticationSchemeOptions, HostAuthenticationHandler>(
         "BasicAuthentication", null);
....
app.UseAuthentication();
services.AddAuthentication(“基本验证”)
.AddScheme(
“基本认证”,空);
....
app.UseAuthentication();
是否可以中断客户端调用并以某种方式向客户端发出特定错误的信号