首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >服务结构和标识服务器4

服务结构和标识服务器4
EN

Stack Overflow用户
提问于 2016-11-23 14:11:14
回答 1查看 1.7K关注 0票数 2

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

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

代码语言:javascript
运行
复制
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在崩溃时为null。然后我试着打电话,但这次是未经授权的,得到了同样的结果。

这是我的api的配置:

代码语言:javascript
运行
复制
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = "http://localhost:19081/App/Identity",
            ScopeName = "api1",
            RequireHttpsMetadata = false,
            AutomaticAuthenticate = true
        });

身份服务器的配置:

代码语言:javascript
运行
复制
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();

我的当事人:

代码语言:javascript
运行
复制
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
            }
        };
    }
}

和范围:

代码语言:javascript
运行
复制
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只存在于某些时候?

第二:

为什么身份服务器在没有授权的情况下没有响应401状态代码,而我在控制器上有授权?

EN

回答 1

Stack Overflow用户

发布于 2020-02-04 12:25:54

当idsrv驻留在负载均衡器后面的多个节点上时,随后的请求会到达不同的节点,您需要为它们提供一致性。

所有实例至少应该为DB实现IPersistedGrantStore IPersistedGrantStore。

我想他们也应该拥有相同的cert (AddSigningCredential)和asp keystore (AddDataProtection).。我建议你从分布式缓存和数据保护开始。您还可以临时实现IPersistedGrantStore就像这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40766735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档