我們正在根據最新的檔案在 MVC5 應用程式中實作“使用 Google 登錄”,這與我們在網路上看到的大多數示例完全不同且更直接。
該程序的一部分是“驗證您的服務器端的 Google ID 令牌”,如本頁所述:https : //developers.google.com/identity/gsi/web/guides/verify-google-id-token
我們在這里被告知“與其撰寫自己的代碼來執行這些驗證步驟,我們強烈建議您為您的平臺使用 Google API 客戶端庫”,這很公平,但是
a) 該頁面上沒有 .net 的代碼示例,b) 專案檔案似乎與以任何方式登錄 Google 無關 c) 如果您實際查看 .net 客戶端庫的 github 在這里:https ://github.com/googleapis/google-api-dotnet-client它說“該客戶端庫受支持,但僅在維護模式下”,這讓我懷疑我們是否打算使用它。
任何人都可以就我們是否應該使用該庫、手動編碼我們的解決方案或使用某種第三方 JWT 庫給我們一些指導嗎?
謝謝閱讀!
uj5u.com熱心網友回復:
我想這就是你要找的。
檢索用戶身份
using Google.Apis.Auth;
using System;
using System.Threading;
using System.Threading.Tasks;
public class IAPTokenVerification
{
/// <summary>
/// Verifies a signed jwt token and returns its payload.
/// </summary>
/// <param name="signedJwt">The token to verify.</param>
/// <param name="expectedAudience">The audience that the token should be meant for.
/// Validation will fail if that's not the case.</param>
/// <param name="cancellationToken">The cancellation token to propagate cancellation requests.</param>
/// <returns>A task that when completed will have as its result the payload of the verified token.</returns>
/// <exception cref="InvalidJwtException">If verification failed. The message of the exception will contain
/// information as to why the token failed.</exception>
public async Task<JsonWebSignature.Payload> VerifyTokenAsync(
string signedJwt, string expectedAudience, CancellationToken cancellationToken = default)
{
SignedTokenVerificationOptions options = new SignedTokenVerificationOptions
{
// Use clock tolerance to account for possible clock differences
// between the issuer and the verifier.
IssuedAtClockTolerance = TimeSpan.FromMinutes(1),
ExpiryClockTolerance = TimeSpan.FromMinutes(1),
TrustedAudiences = { expectedAudience }
};
return await JsonWebSignature.VerifySignedTokenAsync(signedJwt, options, cancellationToken: cancellationToken);
}
}
該庫處于維護模式,因為它已被 Google 視為穩定/已完成。他們只會在發現關鍵問題時對其進行更改。
uj5u.com熱心網友回復:
我希望這個網址對你有幫助:https : //googleapis.dev/dotnet/Google.Apis.Auth/latest/api/Google.Apis.Auth.GoogleJsonWebSignature.html
using Google.Apis.Auth;
using Google.Apis.Auth.OAuth2;
GoogleJsonWebSignature.Payload payload = await GoogleJsonWebSignature.ValidateAsync(Token);
這是一個關于如何驗證您的令牌的小例子。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351554.html
標籤:C# 。网 谷歌登录 google-api-dotnet-client
上一篇:添加華為套件時出現“Couldnotfindcom.huawei.hms:location:6.0.0.302”錯誤
