在我的asp net core 5(api)中,我已将Firebase身份验证与此中间件集成:
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();
}
我已经按照这个教程:https://blog.markvincze.com/secure-an-asp-net-core-api-with-firebase/
它的工作原理:将[Authorize]添加到控制器,我需要传递不记名令牌。现在,在控制器操作中,我需要获取当前用户ID和经过身份验证的电子邮件。
如何从Firebase(通过Firebase进行身份验证的用户)获取用户ID和用户电子邮件?
据我所知,在用户成功登录后,asp.net核心会将令牌中的声明写入用户声明属性。然后你可以从控制器或其他类中的httpcontextaccator中读取它。
更多细节,您可以参考以下代码:
将以下代码添加到Startup ConfigreServices方法中
services.AddHttpContextAccessor();
在控制器中添加以下代码以获取索赔:
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();
}