Asp MVC:如何从 ApplicationUser 获取角色

编程入门 行业动态 更新时间:2024-10-26 22:28:05
本文介绍了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 获取角色

本文发布于:2023-11-25 08:50:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角色   Asp   MVC   ApplicationUser

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!