这可能是因为使用了 gRPC 而不是 HTTP,因此可能需要在 Startup.cs 中添加以下代码:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapGrpcService().RequireAuthorization(); // add this line
endpoints.MapFallbackToFile("index.html");
});
注:MyGrpcService 是自己的 gRPC 服务类名,RequireAuthorization() 方法将强制所有 gRPC 请求进行身份验证。
如果您的 gRPC 服务中的方法需要使用 AllowAnonymous 属性,则应使用以下方法:
[Authorize]
public class MyGrpcService : MyGrpc.MyGrpcBase
{
[AllowAnonymous]
public override Task MyServiceMethod(MyRequest request, ServerCallContext context)
{
// use [AllowAnonymous] attribute here
// your code logic
}
}
注:在 MyGrpcService 类上应使用 [Authorize] 属性,而在需要 AllowAnonymous 的方法上使用 [AllowAnonymous] 属性。
上一篇:BlazorWASMonASP.NETCorewithOpenIddictgRPCunauthorizedrequestsnotworkingevenwith[AllowAnonymous]
下一篇:BlazorWASMPrerenderingwithSignalR-无法实例化实现类型IAccessTokenProvider。