ASP.NET Identity 2.0检查当前用户是否处于角色IsInRole中



使用ASP.NET Identity 2.0,如何检查当前登录的用户是否处于角色中?我正在使用以下内容,但不知道是否有更有效的方法。

var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DbContext()));
var au = um.FindByEmail(Context.User.Identity.GetUserName());
var inrole = um.IsInRole(au.Id, "Admin");
if (inrole)
{
}

ASP Identity中的正确方法与一样简单

User.IsInRole("rolename");

假设您在ASP.NET中,它非常简单:

if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
{
  return "You are not authorized to access this page.";
)

(来自http://msdn.microsoft.com/en-us/library/4z6b5d42%28v=vs.110%29.aspx)

您可以从Identity获取用户id,而不必在数据库中查找用户。。。

var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DbContext()));
var inrole = um.IsInRole(Context.User.Identity.GetUserId(), "Admin");

这对我有用,希望能有所帮助。。。

If HttpContext.Current.User.IsInRole("admin") Then
        adminmnu.Visible = True
    End If

最新更新