小编Ily*_*kin的帖子

Firebase 3:使用.net和c#创建自定义身份验证令牌

我正在尝试使用自定义令牌实施Firebase 3身份验证机制(如https:// firebase.google.com/docs/auth/server/create-custom-tokens中所述).

我的服务器是ASP.NET MVC Application.

因此,根据说明(https://firebase.google.com/docs/server/setup),我为Firebase应用程序创建了一个服务帐户,并生成了一个".p12"格式的密钥.

之后根据此处的说明(https://firebase.google.com/docs/auth/server/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library)我尝试生成自定义令牌并使用之前收到的密钥对其进行签名步.对于令牌生成,我使用了Microsoft的SystemIdentityModel.Tokens.Jwt库,因此代码如下所示:

var now = DateTime.UtcNow;
var tokenHandler = new JwtSecurityTokenHandler();
var key = new X509AsymmetricSecurityKey(new X509Certificate2(p12path, p12pwd));
var signinCredentials = new SigningCredentials(key, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "http://www.w3.org/2001/04/xmlenc#rsa-sha256");
Int32 nowInUnixTimestamp = (Int32)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

var token = tokenHandler.CreateToken(
            issuer: serviceAccountEmail,
            audience: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",                
            signingCredentials: signinCredentials,
            subject: new ClaimsIdentity(new Claim[]
                    {
                    new Claim("sub", serviceAccountEmail),
                    new Claim("iat", nowInUnixTimestamp.ToString()),
                    new Claim("exp", (nowInUnixTimestamp + (60*60)).ToString()),
                    new Claim("uid", uid)
                    })
            );

var tokenString = tokenHandler.WriteToken(token);
Run Code Online (Sandbox Code Playgroud)

然后尝试使用Firebase Javascript SDK在React …

c# asp.net-mvc firebase firebase-authentication

13
推荐指数
1
解决办法
6238
查看次数