【问题标题】:decode firebase token in .net在.net中解码firebase令牌
【发布时间】:2023-04-04 22:30:01
【问题描述】:

我正在我的 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的端点 我可以通过这种方式验证和验证用户。
(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 中解码令牌?

【问题讨论】:

    标签: .net firebase firebase-authentication


    【解决方案1】:

    我已经能够使用 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 中的注释在这里也会有所帮助。

    【讨论】:

    • 这对我不起作用,我尝试了 JWT,它工作正常。
    • @user1892203 - 如果您可以提出一个新问题,详细说明您所看到的内容,那么我会尽我所能提供帮助。您的评论没有提供足够的信息来提供帮助。
    猜你喜欢
    • 2019-06-27
    • 2017-04-11
    • 2021-09-12
    • 2017-02-17
    • 2019-09-24
    • 2017-04-15
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多