Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using protobuf-nt.grpc without .proto files with jwt authentication #325

Open
lindinho-da-mamae opened this issue Feb 27, 2024 · 3 comments

Comments

@lindinho-da-mamae
Copy link

I tried to create a grpc client server project using protobuf-grpc.net with jwt authentication. I even managed to obtain the authentication token, however the examples I find the token must be attached to a metadata and then sent to greetservice which needs servercallcontext to carry out the authentication and it always tells me that it is impossible to convert metadata to servercallcontext. the interesting thing is that without using protobuf-net.grpc but using .proto files it works

IAuthenticationService authenticationClient = Channel.CreateGrpcService<IAuthenticationService>(); AuthenticationRequest authenticationRequest = new AuthenticationRequest { UserName = "admin", Password = "admin" }; Responses.AuthenticationResponse authenticationResponse = await authenticationClient.Authenticate(authenticationRequest); Headers = new Metadata { { "Authorization", $"Bearer {authenticationResponse.AccessToken}" } };
Cliente_pergunta input = new Cliente_pergunta { Name =i.ToString()+ " Lindo" }; Servidor_Responde reply = await Canal.GetInstance().Client.SayHelloAsync(input,Headers);

passing to

public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); }
However, I get the error saying that it is impossible to convert from grpc.Core.Metadata to grpc.Core.ServerCallContext

@mgravell
Copy link
Member

There seems to be a mix of protobuf-net.Grpc code-first style and Google .proto-first style; I want to be 100% sure what the scenario is here - which approach are you using at the server? What would I need to repro this? Protobuf-net.Grpc usually uses CallContext, to allow a shared client/server API, but that exposes a ServerCallContext if required.

@lindinho-da-mamae
Copy link
Author

lindinho-da-mamae commented Feb 27, 2024

I don't know how to explain it but I uploaded the project to github.
https://github.com/lindinho-da-mamae/GrpcJwtAuthentication

here I get the token

` using var channel = GrpcChannel.ForAddress("https://localhost:5004");

    //    var authenticationClient = new Authentication.AuthenticationClient(channel);
    var authenticationClient = channel.CreateGrpcService<IAuthenticationService>();

    var authenticationResponse =await authenticationClient.Authenticate(new AuthenticationRequest
    {
        UserName = "admin",
        Password = "admin"
    });


    if (authenticationResponse.AccessToken.Length >= 1)
    {
        Console.WriteLine("usuario logado com sucesso");
        var headers = new Metadata { { "Authorization", $"Bearer {authenticationResponse.AccessToken}" } };

        var client = channel.CreateGrpcService<IGreeterService>();
        var reply = await client.SayHello(new HelloRequest { Name = "lindinho da mamae" }, headers);
        Console.WriteLine(reply.Message);
    }
    else
    {
        Console.WriteLine("usuario nao autorizado");
        Console.ReadLine(); return;
    }
    await channel.ShutdownAsync();
    Console.WriteLine("GFDGs");
    Console.ReadLine();
}`

and headers, in my understanding, are being passed as metadata, but here

` public class GreeterService : IGreeterService
{
private readonly ILogger _logger;

    public GreeterService(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<GreeterService>();
    }
    [Authorize]
    public  Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply
        {
            Message = "Hello " + request.Name
        });
    }
}`

@lindinho-da-mamae
Copy link
Author

It's not a special project, I just tried to make a mix of two examples I found on the internet, in fact I had made a more elaborate project but I made this simplified one to serve as an example for the problem, sorry for the inconvenience but I'm new to this area

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants