当前位置: 首页 > 编程语言 > C#

AspMVC-HowtoGetRolesfromApplicationUser分享

时间:2023-04-10 20:10:12 C#

AspMVC:HowtoGetRolesfromApplicationUser在ASP.NETMVC5中,在controller中,我使用了发出请求的用户:ApplicationUser用户=系统.Web.HttpContext.Current.GetOwinContext().GetUserManager().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());使用ApplicationUser实例,如何获取用户的所有角色?您可以使用UserManager获取用户和分配的角色。varuserManager=HttpContext.GetOwinContext().GetUserManager();然后你可以像你已经做的那样获得你的用户,你也可以通过调用GetRoles方法userManager.GetRoles(userId);来获得特定用户的角色。列出角色=newUserManager().GetRoles(userIdString)).ToList();下面所需的类是使用VS2015在ASP.NET4.5项目中自动创建的。文件名为IdentityModels.cs。默认安装4个Nuget软件包,包括Microsoft.AspNet.WebApiv5.2.3。以上就是C#学习教程:AspMVC:HowtogettherolesharingfromApplicationUser的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——publicclassUserManager:UserManager{publicUserManager():base(newUserStore(newApplicationDbContext())){}}publicclassApplicationDbContext:IdentityDbContext{publicApplicationDbContext():base("DefaultConnection"){}}publicclassApplicationUser:IdentityUser{}本文来源于网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: