在ASP.NET MVC 5中向Windows角色添加自定义角色
我正在使用ASP.NET MVC 5构建一个Intranet应用程序.
我的目标是对Active Directory所做的任何用户进行身份验证(即我使用“Windows身份验证”),然后将组添加到应用程序内的任何用户(不使用域组). 我在这里找到了一些非常有趣的代码: http://brockallen.com/2013/01/17/adding-custom-roles-to-windows-roles-in-asp-net-using-claims/ 但它在我的场景中不起作用:当我使用[Authorize(Role =“AppRole”)]装饰控制器时,即使用户(使用声明)与“AppRole”角色相关联,我也无法获得授权. 这是我的代码: 在Global.asax.cs中 void Application_PostAuthenticateRequest() { if (Request.IsAuthenticated) { string[] roles = Utils.GetRolesForUser(User.Identity.Name); var id = ClaimsPrincipal.Current.Identities.First(); foreach (var role in roles) { //id.AddClaim(new Claim(ClaimTypes.Role,role.ToString())); id.AddClaim(new Claim(ClaimTypes.Role,@"Kairos.milCompliance")); } bool pippo = User.IsInRole("Compliance"); HttpContext.Current.User = (IPrincipal)id ; bool pippo2 = User.IsInRole("Compliance"); } } 函数GetRolesForUser如下(并且工作正常): public static string[] GetRolesForUser(string username) { dbOrdiniPersonaliEntities db = new dbOrdiniPersonaliEntities(); string utente = StripDomain(username); string[] gruppi = new string[db.vGruppiUtentis.Where(t => t.KairosLogin == utente).Count()]; int i=0; foreach (var gruppo in db.vGruppiUtentis.Where(t => t.KairosLogin == utente)) { gruppi[i]=gruppo.GruppoDes; i=i++; } return gruppi; } 控制器使用“标准”Authorize子句进行修饰: [Authorize(Roles="AppRole")] public ActionResult Index(string sortOrder,string currentFilter,string DesSearchString,int? page) { // my code here } 任何的想法? 提前致谢 UPDATE 谢谢@Leandro void Application_PostAuthenticateRequest() { if (Request.IsAuthenticated) { string[] roles = Utils.GetRolesForUser(User.Identity.Name); ClaimsIdentity id = ClaimsPrincipal.Current.Identities.First(); foreach (var role in roles) { //id.AddClaim(new Claim(ClaimTypes.Role,@"Kairos.milCompliance")); } bool pippo = User.IsInRole("Compliance"); SetPrincipal((IPrincipal)id); bool pippo2 = User.IsInRole("Compliance"); } } 但是当代码到达这一点时,我收到运行时错误 SetPrincipal((IPrincipal)id); 错误如下 无法将类型为“System.Security.Principal.WindowsIdentity”的对象强制转换为“System.Security.Principal.IPrincipal”. 谢谢你的帮助 更新2(可能已解决) 你好 ASP.NET MVC and Windows Authentication with custom roles 根据@Xhalent的回答,我按如下方式修改了我的代码 protected void Application_PostAuthenticateRequest() { if (Request.IsAuthenticated) { String[] roles = Utils.GetRolesForUser(User.Identity.Name); GenericPrincipal principal = new GenericPrincipal(User.Identity,roles); Thread.CurrentPrincipal = HttpContext.Current.User = principal; } } 看来现在工作正常!任何意见?有什么缺点吗?非常感谢!! 解决方法
使用此方法保存主体,因此它也在线程中设置:
private void SetPrincipal(IPrincipal principal) { Thread.CurrentPrincipal = principal; if (HttpContext.Current != null) { HttpContext.Current.User = principal; } } 更新:还允许匿名并测试User.IsInRole是否在方法内部获得了某些内容. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 在ObjectDataSource更新事件期间取消Gridview编
- asp.net – 递归控制搜索与LINQ
- asp.net-mvc – 在Asp MVC Telerik网格和实体框架中处理枚举
- asp.net-mvc – Windows 8 VS2012 IISExpress Windows身份验
- ASP.NET应用程序在3个物理层上
- asp.net – VS2015为Class Library添加引用
- asp.net-mvc – HiddenFor(x => x.Id)正在由UrlParameter而
- ASP.NET Core 依赖注入(DI)
- 如何使用Velocity设计ASP.NET MVC站点进行缓存?
- asp.net – 什么是global.asax用于?