首页 文章

解码.net中的firebase令牌

提问于
浏览
2

我正在Android应用中从signInWithGoogle迁移到Firebase .

我可以在我的Android应用程序中生成firebase用户并提取用户信息,但我无法使用firebase令牌与我的后端服务器进行身份验证 .

我希望能够将令牌传递给服务器,对其进行解码并验证用户,而不是传递电子邮件地址或其他一些个人信息 . 这是文档中首选的身份验证方法

// The user's ID, unique to the Firebase project. Do NOT use this value to // authenticate with your backend server, if you have one. Use //FirebaseUser.getToken() instead.

使用signInWithGoogle,我只需将令牌传递到服务器并在 googleapis.com/oauth2/v3/tokeninfo?id_token=TOKEN 点击 endpoints ,我就可以通过这种方式验证和验证用户 .
https://developers.google.com/identity/sign-in/web/backend-auth

似乎firebase不提供这种能力 . 我能找到的只是适用于Java和NODEjs的SDK . (https://firebase.google.com/docs/auth/server#use_a_jwt_library

有没有办法在.NET中解码令牌?

1 回答

  • 0

    我已经能够使用Org.BouncyCastle(https://www.nuget.org/packages/BouncyCastle/)和Jose.JWT(https://www.nuget.org/packages/jose-jwt/)库在.NET中解码令牌,使用这种方法:

    string tokenToDecode=@"eyGF...._pEkKGg...NnO7w";
    
    StreamReader sr = new StreamReader(GenerateStreamFromString("-----BEGIN PRIVATE KEY-----\nMIIE...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n-----END PRIVATE KEY-----\n"));
    var pr=new Org.BouncyCastle.OpenSsl.PemReader(sr);
    RsaPrivateCrtKeyParameters RsaParams = (RsaPrivateCrtKeyParameters)pr.ReadObject();
    
    string decodedToken=Jose.JWT.Decode(tokenToDecode, Org.BouncyCastle.Security.DotNetUtilities.ToRSA(RsaParams), JwsAlgorithm.RS256);
    

    related question中的注释也会有所帮助 .

相关问题