首先,很抱歉可能会有重复,我相信这个问题已经被问过很多次了,但我找不到明确的答案或如何开始的方向。
我想做的是在安卓上为我们的组织应用进行SSO,我想让它有原生体验(不需要浏览器)。
我已经在 identityserver4上构建了 oidc,并且已经在生产中使用 web 和移动客户端。
我并不是想知道具体的实现细节,只是想知道如何创建一个应用,而不是浏览器来负责认证和会话管理。然后,我可以创建sdk,将其安装在所有的应用程序中,他们将通过这个本地so应用程序共享认证逻辑。就像facebook一样,例如
你所说的原生体验叫做 资源所有者全权证书赠款.
要在IdentityServer4中实现它,您需要实现IResourceOwnerPasswordValidator接口。
public class CustomResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{
public Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
{
//Validate user's username and password. Insert your logic here.
if(context.UserName == "admin" && context.Password == "admin@123")
context.Result = new GrantValidationResult("123", OidcConstants.AuthenticationMethods.Password);
return Task.FromResult(0);
}
}
然后配置IdentityServer4来使用它。
在Startup.cs中添加以下代码
var builder = services.AddIdentityServer()
.AddInMemoryIdentityResources(Config.Ids)
.AddInMemoryApiResources(Config.Apis)
.AddInMemoryClients(Config.Clients)
.AddResourceOwnerValidator<CustomResourceOwnerPasswordValidator>();
并配置一个客户端来使用 资源所有者全权证书赠款.
new Client
{
ClientId = "resourceownerclient",
AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,
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,
"dataEventRecords"
}
}
注意 AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials
行。
这里是 联系 可能是IdentityServer与Microsoft Identity Core的实现。