Reputation: 4087
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)?
Upvotes: 1
Views: 2292
Reputation: 27962
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();
}
Upvotes: 1