gpt4 book ai didi

c# - ASP MVC : How get roles from ApplicationUser

转载 作者:行者123 更新时间:2023-11-30 13:55:24 26 4
gpt4 key购买 nike

在 ASP.NET MVC 5 中,在一个 Controller 中,我让发出请求的用户使用:

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

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

最佳答案

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

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

然后您可以像以前一样获取您的用户,也可以通过调用 GetRoles 方法获取特定用户的角色

userManager.GetRoles(userId);

关于c# - ASP MVC : How get roles from ApplicationUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34975075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com