簡體   English   中英

OWIN Google身份驗證-如何獲取id_token?

[英]OWIN Google authentication - how to get id_token?

我已經進行了設置,以便能夠使用Google成功進行身份驗證。 代碼看起來類似於以下幾行:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions
{
    AuthenticationType = "Google",
    Caption = "Sign-in with Google",
    SignInAsAuthenticationType = signInAsType,
    ClientId = "yyy",
    ClientSecret = "zzz",
    Provider = new GoogleOAuth2AuthenticationProvider
    {
        OnAuthenticated = async context =>
        {
            // How can I access id_token here?
        }
    }
});

調用Authenticated方法時,我可以通過GoogleOAuth2AuthenticatedContext訪問有關該用戶的各種信息。 但是,我需要能夠從Google讀取原始的id_token,以便可以將其添加到聲明中,並在以后的身份驗證流程中對其進行訪問。 有什么辦法可以做到嗎?

首先,請確保您使用的是3.0.1位,因為最初的3.0.0版本不包含此方案所需的TokenResponse屬性。

然后,更新您的OnAuthenticated通知以從令牌響應中提取身份令牌:

var token = context.TokenResponse.Value<string>("id_token");

要解析“ OpenID標識符”,可以使用JWT安全令牌處理程序從身份令牌中提取聲明:

var jwt = new JwtSecurityToken(token);
var identifier = jwt.Claims.FirstOrDefault(claim => string.Equals(claim.Type, "openid_id", StringComparison.InvariantCulture));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM