【问题标题】:Service Fabric and Identity Server 4Service Fabric 和 Identity Server 4
【发布时间】:2016-11-23 14:11:14
【问题描述】:

我是身份服务器的新手,并且在我的开发中设置了它,它主要在使用单个节点时工作。如果我切换到 5 个节点,它有时会工作,有时却不会。

我在控制器上有 Authorize 属性,它扩展了一个基本控制器,该控制器具有从用户声明中获取用户角色的功能。

protected string GetUserRole()
    {
        var roleClaim = User.Claims.SingleOrDefault(c => c.Type == "role");

        if (roleClaim == null)
        {
            throw new ArgumentNullException("Cant find role claim on: " + Request.Host.Host);
        }
        else
        {
            return roleClaim.Value;
        }
   }

当我进行授权调用(在标头中带有标记)时会发生什么,roleClaim 在崩溃时为空。然后我尝试拨打电话,但这次未经授权并得到了相同的结果。

这是我的 api 的配置:

app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = "http://localhost:19081/App/Identity",
            ScopeName = "api1",
            RequireHttpsMetadata = false,
            AutomaticAuthenticate = true
        });

身份服务器的配置:

var cert = new X509Certificate2(Path.Combine(_contentRoot, "damienbodserver.pfx"), "");
        services.AddDeveloperIdentityServer()
            .SetSigningCredential(cert)
            .AddInMemoryScopes(Scopes.Get())
            .AddInMemoryClients(Clients.Get())
            .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()
            .AddProfileService<ProfileService>();

        services.AddMvc();

我的客户:

public static IEnumerable<Client> Get()
    {
        return new[]
        {
            new Client()
            {
                ClientId = "myapi",
                ClientSecrets = new List<Secret>
                {
                    new Secret("secret".Sha256())
                },
                ClientName = "My Beautiful Api",
                AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                AllowAccessTokensViaBrowser = true,
                RequireConsent = false,
                AllowedScopes = {
                    "openid",
                    "api1"
                },
                AllowedCorsOrigins = new List<string> {
                    "*"
                },
                Enabled = true
            }
        };
    }
}

和范围:

public static IEnumerable<Scope> Get()
    {
        return new List<Scope>
        {
            StandardScopes.OpenId,
            StandardScopes.ProfileAlwaysInclude,
            StandardScopes.EmailAlwaysInclude,
            StandardScopes.OfflineAccess,
            StandardScopes.RolesAlwaysInclude,
            new Scope
            {
                Name = "api1",
                DisplayName = "API 1",
                Description = "API 1 features and data",
                Type = ScopeType.Resource,
                ScopeSecrets = new List<Secret>
                {
                    new Secret("secret".Sha256())
                },
                Claims = new List<ScopeClaim>
                {
                    new ScopeClaim("role")
                }
            }
        };
    }

我曾尝试阅读文档,但似乎缺少很多内容,所以我的问题是首先:

为什么roleClaim 只存在于某些时候?

第二个:

为什么 Identity Server 在未授权且我在控制器上有 [Authorize] 时没有响应 401 状态代码?

【问题讨论】:

    标签: azure-service-fabric identityserver4


    【解决方案1】:

    当 idsrv 位于负载均衡器后面的多个节点上时,后续请求会转到不同的节点,您需要为它们提供一致性。

    所有实例都应至少为您的数据库实现 IPersistedGrantStore (AddPersistedGrantStore)。

    我猜他们也应该有相同的证书 (AddSigningCredential) 和 asp 密钥库 (AddDataProtection)。 我建议你从distributed cache and data protection 开始。你也可以临时实现IPersistedGrantStore like here

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 2021-08-02
      • 2017-10-13
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2018-06-27
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多