简体   繁体   English

尝试使用 protobuf-net 通过 gRPC-Web 进行 Azure AD 身份验证

[英]trying to Azure AD authentication with gRPC-Web using protobuf-net

I am trying to Azure AD authentication with gRPC-Web in a blazor webassembly app.我正在 blazor webassembly 应用程序中尝试使用 gRPC-Web 进行 Azure AD 身份验证。 I am using protobuf-net to help me with the serialization.我正在使用 protobuf-net 来帮助我进行序列化。 I am not sure how to pass the token to have the server side recognize it.我不确定如何传递令牌以使服务器端识别它。 this is what I have:这就是我所拥有的:

var headers = new Metadata
               {
                 { "Authorization", $"Bearer {Token}" }
               };

and, I am sending that as a parameter in the method I want to consume并且,我将它作为我想要使用的方法中的参数发送

var result = await Client.CreateCustomer(this.customer, headers);

This is how the service is injected:这是服务注入的方式:

builder.Services.AddTransient(services =>
        {
            var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()));
            var channel = Grpc.Net.Client.GrpcChannel.ForAddress("****", new GrpcChannelOptions { HttpClient = httpClient });
            return channel.CreateGrpcService<Application.Services.ICustomerService<ServerCallContext>>();
        });

This is how the service is published:这是服务的发布方式:

endpoints.MapGrpcService<CustomerService>().RequireAuthorization().EnableGrpcWeb()

and, this is the implementation:并且,这是实现:

public class CustomerService : ICustomerService<ServerCallContext>
{
    [Authorize]
    public async ValueTask<Customer> CreateCustomer(Customer customerDTO, ServerCallContext context) 
    {****}
}

the error I am getting is cannot convert from 'Grpc.Core.Metadata' to 'Grpc.Core.ServerCallContext' which is kind of obvious.我得到的错误是无法从“Grpc.Core.Metadata”转换为“Grpc.Core.ServerCallContext”,这很明显。

The reference I have found uses Metadata but is ServerCallContext the one I am supposed to use https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/metadata so what I am missing, what I am doing wrong, how to properly use both using protobuf-net?我发现的参考使用元数据,但 ServerCallContext 是我应该使用的那个https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/metadata所以我缺少什么,我做错了什么,如何使用 protobuf-net 正确使用两者?

It looks like the problem here is that you're using ServerCallContext in the method signature;看起来这里的问题是您在方法签名中使用了ServerCallContext the underlying gRPC core has separate client/server context APIs, but this is not amenable to use on an agnostic interface, and as such, protobuf-net.Grpc unifies these two APIs, via CallContext .底层 gRPC 核心具有单独的客户端/服务器上下文 API,但这不适用于不可知的接口,因此,protobuf-net.Grpc 通过CallContext统一了这两个 API。 So: instead of:所以:而不是:

async ValueTask<Customer> CreateCustomer(Customer customerDTO, ServerCallContext context)

for the signature, consider:对于签名,请考虑:

async ValueTask<Customer> CreateCustomer(Customer customerDTO, CallContext context)

or或者

async ValueTask<Customer> CreateCustomer(Customer customerDTO, CallContext context = default)

The CallContext API exposes the common server-side and client-side APIs (headers, cancellation, etc) in a single way, or you can use (for example) context.ServerCallContext to get the server-specific API if needed (this will throw an exception if used on a client-context). CallContext API 以单一方式公开常见的服务器端和客户端 API(标头、取消等),或者您可以使用(例如) context.ServerCallContext来获取特定于服务器的 API(如果需要)(这将抛出如果在客户端上下文中使用,则为异常)。 For client-side usage, a CallContext can be constructed from a CallOptions , which is the core gRPC client-side API, for example:对于客户端使用,可以从CallOptions构造CallContext ,这是核心 gRPC 客户端 API,例如:

var result = await service.CreateCustomer(customer, new CallOptions(headers));

I'm open to the idea of allowing CallContext to be created directly from Metadata / CancellationToken etc (allowing var result = await service.CreateCustomer(customer, headers); ) - but it doesn't seem essential .我对允许直接从Metadata / CancellationToken等创建CallContext开放CallContext (允许var result = await service.CreateCustomer(customer, headers); ) - 但它似乎并不重要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 gRPC-Web 和 protobuf-net 在工作服务中添加新方法时出现异常 - Exception when adding new method in a working Service using gRPC-Web and protobuf-net ASP.NET IIS 上的核心 grpc-Web 返回 404 - ASP.NET Core grpc-Web on IIS returns 404 Asp.net:不支持“应用程序/grpc-web” - Asp.net: 'application/grpc-web' is not supported Azure AD 认证与 .NET 核心 Web ZDB974238714CA8DE634A7CE1D083A14 - Azure AD Authentication with .NET Core Web API 无法在.Net Core Web App上使用Azure AD身份验证注销 - Unable to Sign-out using Azure AD Authentication on a .Net Core Web App gRPC-web 服务工作正常,但不使用bloomRPC - gRPC-web service working fine, but not with bloomRPC 使用Work(ASP.NET AD)身份验证的ASP.NET Core Web App在本地进行调试,但在发布到Azure之后无法进行调试 - ASP.NET Core Web App using Work (Azure AD) Authentication works debugging locally, but not after publish to Azure 使用AD身份验证从Azure Web App获取用户 - Obtain user from Azure Web App using AD authentication 使用Azure AD的API后端(.NET Core)中的身份验证基础 - Authentication basics in API Backend (.NET Core) using Azure AD 如何使用 azure Ad 和 .net 核心处理微服务中的身份验证? - How to handle authentication in micro services using azure Ad and .net core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM