【发布时间】: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