C# 用户登录后缓存用户信息

C# 用户登录后缓存用户信息,c#,entity-framework,authentication,caching,asp.net-mvc-5,C#,Entity Framework,Authentication,Caching,Asp.net Mvc 5,我有不同角色的用户。我想根据用户的角色提供受限视图。我的代码中有检查角色的内容: bool isAdmin = UserManager.IsInRole(currentUser.Id,"admin"); bool isEmployee = UserManager.IsInRole(currentUser.Id,"employee"); 要使上述代码正常工作,我需要实例化currentUser。换句话说,我需要捕获当前登录用户的信息。我尝试了类似var user=user.Identity.Ge

我有不同角色的用户。我想根据用户的角色提供受限视图。我的代码中有检查角色的内容:

bool isAdmin = UserManager.IsInRole(currentUser.Id,"admin");
bool isEmployee = UserManager.IsInRole(currentUser.Id,"employee");
要使上述代码正常工作,我需要实例化currentUser。换句话说,我需要捕获当前登录用户的信息。我尝试了类似var user=user.Identity.GetUserId的方法;还有很多其他的,但是找不到工作代码。我将感谢任何帮助。谢谢大家!

更新:


这是我完整的方法代码。在我检查上面的例子之前,你们可能都想看看

public ActionResult Index()
{ 
    var user = User.Identity.GetUserId(); 
    bool isAdmin = UserManager.IsInRole(user, "admin"); 
    bool isEmployee = UserManager.IsInRole(user, "employee"); 
    if (isAdmin)
    { 
        return View(db.Customers.ToList()); 
    }
    else
    { 
        var restricted = db.Customers.Where(c => c.FirstName == "John");
        return View(restricted); 
    } 
} 
[Authorize]属性应在所需的受限actionController方法中实现。下面是一个例子

 [Authorize(Roles="Admin")]
public ActionResult Index()
        {
            return View();
        }
此控制器方法仅限于角色为Admin的用户。此外,相同的action controller方法可以使用不同的授权标记包含两次。

[authorize]属性应该在所需的受限actionController方法中实现。下面是一个例子

 [Authorize(Roles="Admin")]
public ActionResult Index()
        {
            return View();
        }

此控制器方法仅限于角色为Admin的用户。此外,同一个动作控制器方法可以包含两次不同的授权标签。

我不知怎的想出了解决方案。这是工作代码

if(User.IsInRole("Admin"))
{
    return View(db.Customers.ToList());
}
else
{
    return View(db.MyUserInfo.ToList());
}

我不知怎么想出了解决办法。这是工作代码

if(User.IsInRole("Admin"))
{
    return View(db.Customers.ToList());
}
else
{
    return View(db.MyUserInfo.ToList());
}

User.Identity.GetUserId应该可以正常工作。这是一个全新的MVC5项目吗?这就是授权属性的作用。看这是我完整的方法代码。在我检查上面的例子之前,你们可能都想看看。公共操作结果索引{var user=user.Identity.GetUserId;bool isAdmin=UserManager.IsInRoleuser,admin;bool isEmployee=UserManager.IsInRoleuser,employee;ifisAdmin{return Viewdb.Customers.ToList;}否则{var restricted=from c in db.Customers,其中c.FirstName==John select c;return viewstricted;}User.Identity.GetUserId应该可以正常工作。这是一个全新的MVC5项目吗?这就是授权属性的作用。看这是我完整的方法代码。在我检查上面的例子之前,你们可能都想看看。公共操作结果索引{var user=user.Identity.GetUserId;bool isAdmin=UserManager.IsInRoleuser,admin;bool isEmployee=UserManager.IsInRoleuser,employee;ifisAdmin{return Viewdb.Customers.ToList;}否则{var restricted=from c in db.Customers,其中c.FirstName==John select c;return viewstricted;}