【问题标题】:How to create facebook like own native SSO app?如何像自己的本地 SSO 应用一样创建 facebook?
【发布时间】:2020-09-18 23:04:07
【问题描述】:

首先,对于可能的重复,我深表歉意,我确信这个问题以多种形式被问过很多次,但我找不到明确的答案或方向如何开始。

我正在尝试为我们在 android 上的组织应用程序做 sso,我希望它具有本机体验(没有浏览器)。

我在 identityserver4 上构建了 oidc,它已经在使用 Web 和移动客户端进行生产。

我不是在这里询问实现细节,只是一些参考,关于如何创建将负责身份验证和会话管理而不是浏览器的应用程序的好例子。然后我可以创建 sdk,将其安装在所有应用程序中,他们将通过这个本机 sso 应用程序共享身份验证逻辑。就像 facebook 一样,例如

【问题讨论】:

  • 您不会找到明确的答案,因为这不是实现 sso 的合适方式。查看我的回答 here 以获得更详细的解释。
  • 如果您拥有客户端,您可以为具有资源所有者授权的用户提供本机接口。无论如何,如果第三方安装了应用程序,Facebook 允许第三方使用它的本机界面登录,如果没有安装,则允许第三方使用浏览器登录。我也想要。

标签: android single-sign-on identityserver4 openid-connect


【解决方案1】:

您所说的原生体验称为资源所有者凭据授予

要在 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 行。

这里是link,可能是 IdentityServer 与 Microsoft Identity Core 的实现。

这里是演示 repositoryblog

【讨论】:

    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2012-03-29
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多