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);
}

次に (ここで、roles はメンバーが属するロールのカンマ区切りのリストです):

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 に答える