首先,很抱歉可能会有重复,我相信这个问题已经被问了很多次了,以各种形式出现,但我找不到明确的答案或方向如何开始。
我已经在identityserver4上构建了oidc,它已经与web和移动客户端一起投入生产。
我在这里并不要求实现细节,只是一些参考,如何创建应用程序的好例子,它将负责身份验证和会话管理,而不是浏览器。然后我可以创建sdk,将其安装在所有应用程序中,他们将通过这个本地sso应用程序共享身份验证逻辑。就像facebook一样。
发布于 2020-06-04 16:02:01
您所描述的本机体验称为资源所有者凭据授予。
要在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与微软身份核心的实现的link。
这是repository和blog的演示。
https://stackoverflow.com/questions/62118792
复制