使用 Google 和 Firebase 登录 ASP.NET Razor Pages 应用程序

问题描述 投票:0回答:1

我正在尝试创建一个 ASP.NET razor 页面应用程序来使用 Google 和 firebase 身份验证登录并访问一些 firebase 信息。我已经使用谷歌脚本登录来获取 ID 令牌。我已将其传回我的 razor 页面,并使用 FirebaseAuthentication.net 包,尝试使用凭据登录。

<script>
    function handleCredentialResponse(response) {
        console.log("Encoded JWT ID token: " + response.credential);
        fetch('/login?handler=google_signin&token=' + response.credential, {
            method:'POST',
            body: JSON.stringify({"token": response.credential}),
            headers:
            {
                "RequestVerificationToken": '@GetAntiXsrfRequestToken()'
            },
        })
            .then(response => response.json())
            .then(response => console.log(JSON.stringify(response)))
    }
    window.onload = function () {
        google.accounts.id.initialize({
            client_id: "myid.apps.googleusercontent.com",
            callback: handleCredentialResponse
        });
        google.accounts.id.renderButton(
            document.getElementById("buttonDiv"),
            { theme: "outline", size: "large" }  // customization attributes
        );
    }
</script>



public async Task<JsonResult> OnPostGoogle_Signin(string token)
        {                
            try
            {
                var x = await _firebaseAuth.SignInWithCredentialAsync(GoogleProvider.GetCredential(token));
            }
            catch (Exception ex)
            {

                throw;
            }               

            Console.WriteLine(token);
            return new JsonResult("{'token':" + token + "}");
        }

这是我收到的错误:

Firebase.Auth.FirebaseAuthHttpException: Exception occured during Firebase Http request.
Url: https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=xxxxx
Request Data: {"requestUri":"https://myapp.firebaseapp.com","postBody":"access_token=myaccesstoken&providerId=google.com","returnIdpCredential":true,"returnSecureToken":true}
Response: {
  "error": {
    "code": 400,
    "message": "INVALID_IDP_RESPONSE : Unsuccessful check authorization response from Google: {\n  \"error_description\": \"Invalid Value\"\n}\n",
    "errors": [
      {
        "message": "INVALID_IDP_RESPONSE : Unsuccessful check authorization response from Google: {\n  \"error_description\": \"Invalid Value\"\n}\n",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}

我确信令牌中有一些我不理解的地方,或者那里有什么东西。对我做错了什么有什么想法吗?

firebase-authentication google-oauth
1个回答
0
投票

你下定决心了吗?我开始使用 asp.net 和 google 身份并希望避免同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.