从asp.net核心api向firebase auth添加用户声明
给定一个asp.net核心api和仅使用[Authorize]属性的工作firebase auth,我如何向令牌添加自定义声明以使用[Authorize(Policy=“admin”)?AdminSDK仅适用于node.js、Java、python或go,我找不到任何关于如何直接使用Firebase admin API的文档。据我所知,用户声明必须存储在Firebase Auth后端。据我所知,使用C#Firebase Admin SDK是不可能的。 我也有同样的问题,首先,我使用令牌获取用户id,从数据库中检索用户id的角色,设置声明数组,并使用添加的声明创建自定义令牌。然后我通过他们的RESTAPI将其发送给Firebase,以获得常规Firebase ID和刷新令牌,如中所述。最后我把代币给了客户。 但是,我遇到了一些问题(主要是404个错误),因为没有很好的文档记录您需要对验证中间件进行哪些更改以接受新的重新发布的令牌,而且我从未能够获得刷新令牌,因为它从未出现在REST API响应中。我甚至按照中的步骤,按照Firebase的说明铸造了自己的自定义令牌,但始终无法使这该死的东西正常工作 所以我最终做了一些不同的事情。我从firebase IdToken检索了用户声明,并使用System.IdentityModel.Tokens.Jwt创建了自己的令牌。在那里,我添加了自己的声明和角色,并将其发送给客户。现在客户端使用FireBase登录,但我完全可以控制令牌以使用我的API 这就是我修改启动的方式。请记住,我没有玩所有的选择从asp.net核心api向firebase auth添加用户声明,firebase,firebase-authentication,asp.net-core-webapi,Firebase,Firebase Authentication,Asp.net Core Webapi,给定一个asp.net核心api和仅使用[Authorize]属性的工作firebase auth,我如何向令牌添加自定义声明以使用[Authorize(Policy=“admin”)?AdminSDK仅适用于node.js、Java、python或go,我找不到任何关于如何直接使用Firebase admin API的文档。据我所知,用户声明必须存储在Firebase Auth后端。据我所知,使用C#Firebase Admin SDK是不可能的。 我也有同样的问题,首先,我使用令牌获取用户i
var key = System.Text.Encoding.UTF8.GetBytes(Configuration["MyToken:Key"]);
services.AddAuthentication(
auth => {
auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
auth.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;})
.AddJwtBearer(options => {
options.RequireHttpsMetadata = false;
options.IncludeErrorDetails = true;
options.TokenValidationParameters = new TokenValidationParameters {
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true
};
});
这是简化的编码功能:
public string EncodeTokenMS(string uid, Claim[] claims)
{
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(<Secret key here>));
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256Signature);
var now = DateTimeOffset.UtcNow;
var _claims = new[] {
new Claim(JwtRegisteredClaimNames.Iss, "https://auth.xxx.com"),
new Claim(JwtRegisteredClaimNames.Aud, "https://auth.xxx.com"),
new Claim(JwtRegisteredClaimNames.Sub, uid),
new Claim(JwtRegisteredClaimNames.AuthTime, now.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64),
new Claim(JwtRegisteredClaimNames.Iat, now.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64),
new Claim(JwtRegisteredClaimNames.Exp, now.AddMinutes(60).ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64),
// TODO Actually write the code to get tenancy, roles, and user info
new Claim("tenantId", "PippoBaudo" ),
new Claim(ClaimTypes.Role, "User")
};
//Create and sign the JWT, and write it to a string
var jwt = new JwtSecurityToken(
claims: _claims,
signingCredentials: signingCredentials);
return new JwtSecurityTokenHandler().WriteToken(jwt);
}
public string EncodeTokenMS(string uid,Claim[]claims)
{
var-signingKey=new-SymmetricSecurityKey(Encoding.UTF8.GetBytes());
var signingCredentials=新的signingCredentials(signingKey,SecurityAlgorithms.HmacSha256Signature);
var now=DateTimeOffset.UtcNow;
var_索赔=新[]{
新索赔(JwtRegisteredClaimNames.Iss,“https://auth.xxx.com"),
新索赔(JwtRegisteredClaimNames.Aud,“https://auth.xxx.com"),
新索赔(JwtRegisteredClaimNames.Sub,uid),
新声明(JwtRegisteredClaimNames.AuthTime,now.ToUnixTimeSeconds().ToString(),ClaimValueTypes.Integer64),
新声明(JwtRegisteredClaimNames.Iat,now.ToUnixTimeSeconds().ToString(),ClaimValueTypes.Integer64),
新声明(JwtRegisteredClaimNames.Exp,now.AddMinutes(60).ToUnixTimeSeconds().ToString(),ClaimValueTypes.Integer64),
//TODO实际编写代码以获取租约、角色和用户信息
新索赔(“tenantId”、“PippoBaudo”),
新索赔(ClaimTypes.Role,“用户”)
};
//创建JWT并对其签名,然后将其写入字符串
var jwt=新的JwtSecurityToken(
索赔:_索赔,
签名凭证:签名凭证);
返回新的JwtSecurityTokenHandler().WriteToken(jwt);
}
这种方法的缺点是,当用户注册时,会增加一个往返行程,但是只有当用户需要令牌时,它才应该增加大量延迟,并且我可以将令牌的生存时间增加到60分钟以上
欢迎对此方法提出建议和(建设性)意见
希望这有帮助 自Firebase Admin.NET SDK 1.1发布以来,支持使用自定义声明的控制访问功能 您可以在此处找到存储库: 安装指南和文档: 解决方案:
// Initialize the admin SDK with your service account keys
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile("path/to/serviceAccountKey.json"),
});
// Create the custom user claim that has the role key
var claims = new Dictionary<string, object>
{
{ ClaimTypes.Role, "Administrator" }
};
// This will call the Firebase Auth server and set the custom claim for the user
await FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid, claims);
//使用您的服务帐户密钥初始化管理SDK
FirebaseApp.Create(新建AppOptions()
{
Credential=GoogleCredential.FromFile(“path/to/serviceAccountKey.json”),
});
//创建具有角色密钥的自定义用户声明
var声明=新字典
{
{ClaimTypes.Role,“管理员”}
};
//这将调用Firebase身份验证服务器并为用户设置自定义声明
等待FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(uid,声明);
您找到方法了吗?有一个.net sdk,但在编写本文时只支持一小部分函数:。