Firebase 身份验证 asp.net 核心 [英] Firebase authentication asp.net core
本文介绍了Firebase 身份验证 asp.net 核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
成功登录 Firebase 后,我们收到了 JWT 令牌.
After a successful sign-in to Firebase we received a JWT token.
为了向我的 asp.net 应用程序添加授权,我尝试向我的中间件添加 JwtBearerAuthentication.
In order to add authorization to my asp.net app, I tried to add a JwtBearerAuthentication to my middleware.
我尝试了以下 JwtBearerOptions :
I have tried the following JwtBearerOptions :
var options = new JwtBearerOptions
{
Audience = "myApp",
Authority = "https://securetoken.google.com"
};
和
var options = new JwtBearerOptions
{
Audience = "myApp",
Authority = "https://securetoken.google.com/myApp"
};
不幸的是,这不起作用.我的权威网址可能不正确.
Unfortunately this is not working. My Auhtority url is probably incorrect.
有谁知道Auhtority url是正确的吗?
Does anyone know wich Auhtority url is correct?
推荐答案
JWT 验证需要手动:来源
The JWT validation need to be manual : source
以下代码正在验证 FirebaseToken (JWT):
The following code is validating the FirebaseToken (JWT) :
//Download certificates from google
HttpClient client = new HttpClient();
var jsonResult = client.GetStringAsync("https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com").Result;
//Convert JSON Result
var x509Metadata = JObject.Parse(jsonResult)
.Children()
.Cast<JProperty>()
.Select(i => new x509Metadata(i.Path, i.Value.ToString()));
//Extract IssuerSigningKeys
var issuerSigningKeys = x509Metadata.Select(s => s.X509SecurityKey);
//Setup JwtTokenHandler
var handler = new JwtSecurityTokenHandler();
SecurityToken token;
handler.ValidateToken(user.FirebaseToken, new TokenValidationParameters
{
IssuerSigningKeys = issuerSigningKeys,
ValidAudience = "myApp",
ValidIssuer = "https://securetoken.google.com/myApp",
IssuerSigningKeyResolver = (arbitrarily, declaring, these, parameters) => issuerSigningKeys
}, out token);
public class x509Metadata
{
public string KID { get; set; }
public string Certificate { get; set; }
public X509SecurityKey X509SecurityKey { get; set; }
public x509Metadata(string kid, string certificate)
{
KID = kid;
Certificate = certificate;
X509SecurityKey = BuildSecurityKey(Certificate);
}
private X509SecurityKey BuildSecurityKey(string certificate)
{
//Remove : -----BEGIN CERTIFICATE----- & -----END CERTIFICATE-----
var lines = certificate.Split('
');
var selectedLines = lines.Skip(1).Take(lines.Length - 3);
var key = string.Join(Environment.NewLine, selectedLines);
return new X509SecurityKey(new X509Certificate2(Convert.FromBase64String(key)));
}
}
这篇关于Firebase 身份验证 asp.net 核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文