[英]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.