gpt4 book ai didi

identityserver4 - Identity Server 4 - 资源所有者密码授予和 Google 身份验证

转载 作者:行者123 更新时间:2023-12-04 17:48:15 30 4
gpt4 key购买 nike

我有一个应用程序当前使用资源所有者密码授予类型来允许用户通过单页应用程序登录。身份服务器目前托管在与 Web API 相同的项目中。但是,我们希望为用户添加使用其 Google 帐户注册/登录的功能。目前,用户数据存储在表中并由 ASP.NET Core Identity 管理。

有没有一种方法既可以在应用程序中为“本地”用户提供资源所有者密码授予类型,又可以通过 Google 启用第三方身份验证?目前,我们使用用户名和密码访问 Identity Server token 端点,并将 token 存储在浏览器中。然后它被传递到任何需要授权的端点。在集成 Google 身份验证和检索 Google token 时,同样的流程是否仍然有效?

最佳答案

所有功劳归功于 Behrooz Dalvandithis很棒的帖子。

此问题的解决方案是创建自定义授权并实现 IExtensionGrantValidator。

public class GoogleGrant : IExtensionGrantValidator
{
private readonly IGoogleService _googleService;
private readonly IAccountService _accountService;

public GoogleGrant(IGoogleService googleService, IAccountService accountService)
{
_googleService = googleService;
_accountService = accountService;
}
public string GrantType
{
get
{
return "google_auth";
}
}

public async Task ValidateAsync(ExtensionGrantValidationContext context)
{
var userToken = context.Request.Raw.Get("id_token");

if (string.IsNullOrEmpty(userToken))
{
//You may want to add some claims here.
context.Result = new GrantValidationResult(TokenErrors.InvalidGrant, null);
return;
}
//Validate ID token
GoogleJsonWebSignature.Payload idTokenData = await _googleService.ParseGoogleIdToken(userToken);

if (idTokenData != null)
{
//Get user from the database.
ApplicationUser user = await _accountService.FindByEmail(idTokenData.Email);

if(user != null)
{
context.Result = new GrantValidationResult(user.Id, "google");
return;
}
else
{
return;
}

}
else
{
return;
}
}
}

在 Startup 中配置这个验证器

            var builder = services.AddIdentityServer()
.AddInMemoryIdentityResources(Config.Ids)
.AddInMemoryApiResources(Config.Apis)
.AddInMemoryClients(Config.Clients)
.AddResourceOwnerValidator<CustomResourceOwnerPasswordValidator>()
.AddExtensionGrantValidator<GoogleGrant>();//Custom validator.

最后但并非最不重要的一点。在配置文件中添加以下代码。请注意“google_auth”授权类型。

new Client
{
ClientId = "resourceownerclient",

AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials.Append("google_auth").ToList(),
AccessTokenType = AccessTokenType.Jwt,
AccessTokenLifetime = 3600,
IdentityTokenLifetime = 3600,
UpdateAccessTokenClaimsOnRefresh = true,
SlidingRefreshTokenLifetime = 30,
AllowOfflineAccess = true,
RefreshTokenExpiration = TokenExpiration.Absolute,
RefreshTokenUsage = TokenUsage.OneTimeOnly,
AlwaysSendClientClaims = true,
Enabled = true,
ClientSecrets= new List<Secret> { new Secret("dataEventRecordsSecret".Sha256()) },
AllowedScopes = {
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.OfflineAccess,
"api1"
}
}

关于identityserver4 - Identity Server 4 - 资源所有者密码授予和 Google 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47291734/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com