我知道这个问题被问了很多次。我尝试了所有解决方案,但没有成功。我使用 MVC4 并使用表单身份验证。我没有使用会员资格,但想在表单身份验证中存储角色。我正在使用下面的代码来实现我的目标。
FormsAuthentication.SetAuthCookie(result.ID.ToString(), false);
GenericPrincipal gp = new GenericPrincipal(HttpContext.User.Identity, new string[] { "admin" });
HttpContext.User = gp;
我正在成功使用用户名和 IsAuthenticated User.Identity.Name
,User.Identity.IsAuthenticated
但是当我尝试使用代码检查角色时,var role = User.IsInRole("admin");
它返回 false。
请帮忙。