Asp MVC:如何从 ApplicationUser 获取角色 [英] Asp MVC: How get roles from ApplicationUser

查看:25
本文介绍了Asp MVC:如何从 ApplicationUser 获取角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET MVC 5 中,在控制器中,我使用了发出请求的用户:

In ASP.NET MVC 5, in a controller, I have take the user that has make the request with:

ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

使用 ApplicationUser 实例,如何获取用户的所有角色?

With the ApplicationUser instance, how can i get all the Roles of the user?

推荐答案

您可以使用 UserManager 获取用户和分配的角色.

You can get user and assigned roles by using UserManager.

var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

然后你可以像你已经做的那样获取你的用户,你也可以通过调用 GetRoles 方法获取特定用户的角色

and then you can get your user like you already did, and also you can get roles for particular user by calling GetRoles method

userManager.GetRoles(userId);

这篇关于Asp MVC:如何从 ApplicationUser 获取角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆