0

在调试器中,如果我深入 User 对象,我可以看到当前成员的 UserData 属性,((System.Web.Security.FormsIdentity)(User.Identity)).Ticket.UserData其中包含“admin”。

User.Identity.IsAuthenticated有效,User.IsInRole("admin")返回 false。

如果“admin”在 UserData 属性中,那么 User.IsInRole("admin") 不应该返回 true 吗?

更新

我像这样设置 FormsAuthenticationTicket:

public static string CreateEncryptedTicket(string username, string roles, DateTime expireAt, bool isPersistent = true) {
    var ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, expireAt, isPersistent, roles, FormsAuthentication.FormsCookiePath);
    return FormsAuthentication.Encrypt(ticket);
}

然后(其中角色是成员所在角色的逗号分隔列表):

var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, MemberService.CreateEncryptedTicket(member.Id, roles, expireDate));
HttpContext.Response.Cookies.Add(cookie);
4

1 回答 1

0

如果您像这样列出用户的角色,您会看到什么?

public ActionResult ShowUserRoles() {
    string[] roles = Roles.GetRolesForUser();
    // Just hover your mouse over roles above since you're debugging...
    return View(roles); // This view probably doesn't exist.
}
于 2012-08-16T13:46:36.553 回答