Replies: 3 comments 6 replies
-
If your service expects only authorized requests, you should. See example, https://github.com/max-ieremenko/ServiceModel.Grpc/tree/master/Examples/JsonWebTokenAuthentication |
Beta Was this translation helpful? Give feedback.
-
The first example is based on Grpc.Core.Server, which is kind of deprecated. The second example is based on Grpc.AspNetCore.Server and ASP.NET where you have all functionality out of the box. |
Beta Was this translation helpful? Give feedback.
-
max-ieremenko gave the answer for my question. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In another grpc project grpc-dotnet, in server side, there is a method which is generated by proto compiler use token as a parameter.
[https://github.com/grpc/grpc-dotnet/tree/master/examples/Ticketer]
In client side, it can be used like below:
Metadata? headers = null;
if (token != null)
{
headers = new Metadata();
headers.Add("Authorization", $"Bearer {token}");
}
var response = await client.BuyTicketsAsync(new BuyTicketsRequest { Count = 1 }, headers);
The mothed BuyTicketsAsync is generated by proto compiler because it use .proto in server-side , but we have no .proto files.
Its original method has a "Authorize" attribute, like below:
Can I use the Authorize attibute like that using serviceModel.grpc?
[Authorize]
public override Task BuyTickets(BuyTicketsRequest request, ServerCallContext context)
{
var user = context.GetHttpContext().User;
}
Beta Was this translation helpful? Give feedback.
All reactions