1

In my asp net core 5 (api) i have integrated firebase authentication with this middleware:

public void ConfigureServices(IServiceCollection services)
{
   services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.Authority = "https://securetoken.google.com/my-project-id";
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = true,
            ValidIssuer = "https://securetoken.google.com/my-project-id",
            ValidateAudience = true,
            ValidAudience = "my-project-id",
            ValidateLifetime = true
        };
    });
  services.AddControllers();
}

I have follow this tutorial: https://blog.markvincze.com/secure-an-asp-net-core-api-with-firebase/

It works: adding [Authorize] to the controller, i need to pass the bearer token. Now, in the controller action, i need to get the current user id and email that is authenticated.

How can i get the user id and user email from firebase (the user that is authenticated through firebase)?

1
  • 1
    Every token has claims, claims are things are in the token like the id , the email and stuff like that , you have to make sure that google send backs the claims you want and get the claim by its key through the HTTPAccessor Commented Mar 18, 2021 at 12:09

1 Answer 1

1

As far as I know, after the user has successfully login in, the asp.net core will write the claims from the token into user claims property. Then you could read it from the httpcontextaccessor in the controller or other class.

More details, you could refer to below codes:

Add below codes into Startup ConfigureServices method

        services.AddHttpContextAccessor(); 

In the controller add below codes to get the claims:

    private readonly ILogger<HomeController> _logger;
    private readonly IHttpContextAccessor httpContextAccessor;

    public HomeController(ILogger<HomeController> logger, IHttpContextAccessor _httpContextAccessor)
    {
        _logger = logger;
        httpContextAccessor = _httpContextAccessor;
    }

    public IActionResult Index()
    {
        var re=  httpContextAccessor.HttpContext.User.Claims.ToList();

        re.Select(x => x.Type == "Here type in the type of the claims like Email or else" ).FirstOrDefault();

     
        return View();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.