Asp.net mvc 4 User.IsInRole()不在MVC 4中工作

Asp.net mvc 4 User.IsInRole()不在MVC 4中工作,asp.net-mvc-4,isinrole,Asp.net Mvc 4,Isinrole,我知道这个问题被问了很多次。我尝试了所有的解决方案,但都没有成功。我使用MVC4和表单身份验证。我不使用成员身份,但希望在表单身份验证中存储角色。我使用下面的代码来实现我的目标 FormsAuthentication.SetAuthCookie(result.ID.ToString(), false); GenericPrincipal gp = new GenericPrincipal(HttpContext.User.Identity, new string

我知道这个问题被问了很多次。我尝试了所有的解决方案,但都没有成功。我使用MVC4和表单身份验证。我不使用成员身份,但希望在表单身份验证中存储角色。我使用下面的代码来实现我的目标

 FormsAuthentication.SetAuthCookie(result.ID.ToString(), false);
                GenericPrincipal gp = new GenericPrincipal(HttpContext.User.Identity, new string[] { "admin" });
                HttpContext.User = gp;
我正在使用User.Identity.Name和User.Identity.IsAuthenticated成功获取用户名和身份验证,但当我尝试使用代码var role=User.IsInRoleadmin检查角色时;它返回错误


请帮忙。

你把角色放在哪里?@Davidansey我对角色不太了解。我在谷歌上查看了如何在不使用会员资格的情况下使用角色,得到了上面的代码。除了角色外,一切正常。如果您会看到,我已经在GenericPrinciple中提到了管理员角色。