我在理解声明时遇到问题,尤其是角色。
下面给了我分配给用户的两个角色
var roles = UserManager.GetRolesAsync(user.Id).Result;
但是当我得到声明并遍历它时,我只得到第一个角色。我没有得到这两个角色。请注意,我在登录时没有在声明中设置任何角色。
Action 代码
IEnumerable<Claim> claims = null;
var identity = HttpContext.User.Identity as ClaimsIdentity;
if (identity != null && identity.Claims != null && identity.Claims.Any())
{
claims = identity.Claims;
}
return View(claims);
以及对应的 View 代码
@model IEnumerable<System.Security.Claims.Claim>
@{
ViewBag.Title = "Display Claims";
}
<h2>Display Claims</h2>
@if (Model == null)
{
<p class="alert-danger">No claims found</p>
}
else
{
<table class="table table-bordered">
<tr>
<th>Subject</th>
<th>Issuer</th>
<th>Type</th>
<th>Value</th>
</tr>
@foreach (var claim in Model.OrderBy(x => x.Type))
{
<tr>
<td>@claim.Subject.Name</td>
<td>@claim.Issuer</td>
<td>@Html.ClaimType(claim.Type)</td>
<td>@claim.Value</td>
</tr>
}
</table>
}
这是输出。我在这里缺少什么?
表格有两个作用
更新#1
我添加了名字和姓氏作为远程声明,登录后这两个角色现在都在显示。我没有改变任何东西。所以现在我更困惑了...
这里是添加远程声明的提供者
public static class ClaimsUserInfoProvider
{
public static IEnumerable<Claim> GetClaims(ClaimsIdentity user, ApplicationUser applicationUser)
{
var claims = new List<Claim>();
claims.Add(CreateClaim(ClaimTypes.GivenName, applicationUser.FirstName + " " + applicationUser.LastName));
return claims;
}
private static Claim CreateClaim(string type, string value)
{
return new Claim(type, value, ClaimValueTypes.String, "RemoteClaims");
}
}
以及使用声明提供程序的登录操作
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user == null)
{
ModelState.AddModelError("", "Invalid user name or password.");
}
else
{
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
//add claims
identity.AddClaims(ClaimsUserInfoProvider.GetClaims(identity, user));
AuthenticationManager.SignOut();
AuthenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = model.RememberMe
}, identity);
if (!String.IsNullOrEmpty(model.ReturnUrl))
{
return Redirect(model.ReturnUrl);
}
return RedirectToAction("Index", "Home");
}
}
return View(model);
}
最佳答案
很难确定,但我认为这里发生的是声明缓存在用于验证用户身份的 cookie 中。当用户首次登录时,会从数据库中读取声明,并使用声明创建一个 cookie 并将其存储在用户浏览器中。所有进一步的请求都从 cookie 中读取用户声明信息,直到它过期。我在 ASP.NET Identity Cookie Expiration 上写了一篇详细的博客文章有关如何管理过期的更多信息。
您的一些措辞表明(我只是猜测)角色是在用户登录后添加的,因此角色没有添加到 cookie 中,也不会打印出来。当您添加代码以添加名称时,声明会刷新,原因如下:
- cookie 已过期,您必须重新登录。
- 您注销(删除了 cookie)然后重新登录。
您保持登录状态,但是当您重新发布到
login
操作时,您需要调用signout
然后signin
刷新 cookies :AuthenticationManager.SignOut(); AuthenticationManager.SignIn(新的 AuthenticationProperties
您可以复制您遇到的行为:
- 确保用户已退出
- 从
AspNetUserRoles
表中删除用户的角色 - 让用户登录
- 将角色添加回
AspNetUserRoles
表中的用户(手动或通过您管理用户角色的应用程序的某些操作) - 打印出角色
- 您不会在打印输出中看到角色。
- 接下来将用户注销并重新登录,然后您将看到预期的角色。
每次添加角色或声明时,您都需要让用户手动注销,或者您可以像我之前提到的那样调用刷新 cookie。这answer here提供有关如何有效刷新 cookie 的一些上下文。
关于c# - ASP.NET 身份声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33949326/