我对身份服务器很陌生,并且在我的开发中设置了它,并且它主要在使用单个节点时工作。如果我切换到5个节点,它有时工作,有时不工作。
我在控制器上有授权属性,它扩展了一个基本控制器,该控制器具有从用户声明中获取用户角色的函数。
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的配置:
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只存在于某些时候?
第二:
为什么身份服务器在没有授权的情况下没有响应401状态代码,而我在控制器上有授权?
发布于 2020-02-04 12:25:54
当idsrv驻留在负载均衡器后面的多个节点上时,随后的请求会到达不同的节点,您需要为它们提供一致性。
所有实例至少应该为DB实现IPersistedGrantStore IPersistedGrantStore。
我想他们也应该拥有相同的cert (AddSigningCredential)和asp keystore (AddDataProtection).。我建议你从分布式缓存和数据保护开始。您还可以临时实现IPersistedGrantStore就像这里
https://stackoverflow.com/questions/40766735
复制相似问题