检查 MVC5 上基于 Owin 的声明
Check Owin based claim on MVC5
我的 User.IsInRole("CanChangeData")
不工作,但我可以在调试菜单中看到值 CanChangeData
在用户的声明列表中。
如果用户无法更改布局中的数据,我想删除菜单,if
return 错误。 Request.IsAuthenticated
return 正确。
这就是我在 AuthenticationController
上向用户添加声明的方式
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, input.Username),
},
DefaultAuthenticationTypes.ApplicationCookie,
ClaimTypes.Name, ClaimTypes.Role);
var employe = db.Employes.Single(k => k.User == input.Username);
foreach (var permission in employe.Role.Permissions)
{
identity.AddClaim(new Claim(ClaimTypes.Role, permission.Nom));
}
为什么 User.IsInRole("CanChangeData")
没有收到声明?
当您使用声明时,以此为例,您可以这样添加到声明中:
identity.AddClaim(new Claim("ThisIsTheClaimID", "This is the value"));
然后您可以使用下面的代码进行检索:
var myClaimValue = User.FindFirst("ThisIsTheClaimID").Value
希望这有帮助吗?
您可以像下面的代码一样获取相关的声明值,
var identity = (ClaimsIdentity) User.Identity;
var claims = identity.Claims.ToList();
if (claims.Any(x = > x.ClaimType == ClaimTypes.Role && x.ClaimValue == "CanChangeData"))
{
...
}
我使用以下代码按类型获取声明:
public string GetClaimByClaimType(string claimType)
{
return ((ClaimsPrincipal) Thread.CurrentPrincipal)
.Claims
.Where(c => claimType == c.Type)
.Select(c => c.Value)
.SingleOrDefault() ?? string.Empty;
}
在您的特定情况下,因为您会这样调用它:
// I would recommend using a custom claim type instead of the MS schema name
var hasCanChangeDataRole =
GetClaimByClaimType("http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
var canChangeData =
"canChangeData".Equals(hasRole, StringComparison.OrdinalIgnoreCase);
根据您的情况,您可以将这些抽象为更简洁的方法。通常我在声明中存储更复杂的数据类型,因此我进行这些检查的支持方法更加定制化。
我的 User.IsInRole("CanChangeData")
不工作,但我可以在调试菜单中看到值 CanChangeData
在用户的声明列表中。
如果用户无法更改布局中的数据,我想删除菜单,if
return 错误。 Request.IsAuthenticated
return 正确。
这就是我在 AuthenticationController
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, input.Username),
},
DefaultAuthenticationTypes.ApplicationCookie,
ClaimTypes.Name, ClaimTypes.Role);
var employe = db.Employes.Single(k => k.User == input.Username);
foreach (var permission in employe.Role.Permissions)
{
identity.AddClaim(new Claim(ClaimTypes.Role, permission.Nom));
}
为什么 User.IsInRole("CanChangeData")
没有收到声明?
当您使用声明时,以此为例,您可以这样添加到声明中:
identity.AddClaim(new Claim("ThisIsTheClaimID", "This is the value"));
然后您可以使用下面的代码进行检索:
var myClaimValue = User.FindFirst("ThisIsTheClaimID").Value
希望这有帮助吗?
您可以像下面的代码一样获取相关的声明值,
var identity = (ClaimsIdentity) User.Identity;
var claims = identity.Claims.ToList();
if (claims.Any(x = > x.ClaimType == ClaimTypes.Role && x.ClaimValue == "CanChangeData"))
{
...
}
我使用以下代码按类型获取声明:
public string GetClaimByClaimType(string claimType)
{
return ((ClaimsPrincipal) Thread.CurrentPrincipal)
.Claims
.Where(c => claimType == c.Type)
.Select(c => c.Value)
.SingleOrDefault() ?? string.Empty;
}
在您的特定情况下,因为您会这样调用它:
// I would recommend using a custom claim type instead of the MS schema name
var hasCanChangeDataRole =
GetClaimByClaimType("http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
var canChangeData =
"canChangeData".Equals(hasRole, StringComparison.OrdinalIgnoreCase);
根据您的情况,您可以将这些抽象为更简洁的方法。通常我在声明中存储更复杂的数据类型,因此我进行这些检查的支持方法更加定制化。