asp.net-mvc – 将角色添加到ADFS IPrincipal
我几天来一直在寻找这个问题的答案,但我没有找到任何成功.我会发布链接,但它可能会占用整个页面.
所以这就是我所拥有的…… 我有一个MVC应用程序,它使用WC-Federation协议.我已经能够配置应用程序,以便它对用户进行身份验证,并从ADFS返回声明.这很完美.我也可以提取所有索赔,没有任何问题.但我在控制器中的一个操作中执行此操作. 这就是我想要做的…… 我想使用ADFS对用户进行身份验证,但我想使用自己的内部角色来授权用户访问特定的控制器(例如[Authorize(Roles =“CoolRole”)]).我希望能够这样做,因为我已经有一个使用OAuth 2.0的Web API,后端SQL Server数据库来管理用户和角色(内部和外部用户).我现在想要一个允许内部用户的安全门户通过单点登录体验访问数据.看一下Controller模型,我注意到有一些与认证过程相关的属性(OnAuthentication,OnAuthenticationChallenge)和一个用于授权过程的属性(OnAuthorization.) 我不一定需要代码,但我觉得我已经打了一块砖,我需要指向正确的方向. UPDATE 我试过这个: protected override void OnAuthorization( System.Web.Mvc.AuthorizationContext filterContext) { //Private class to create a new IPrincipal based on my AppUserMgr var user = _setCurrentUser( (ClaimsIdentity)filterContext.HttpContext.User.Identity); filterContext.HttpContext.User = user; base.OnAuthorization(filterContext); } 这返回了401(未授权)响应. 和… protected override void OnAuthentication( System.Web.Mvc.Filters.AuthenticationContext filterContext) { //Private class to create a new IPrincipal based on my AppUserMgr var user = _setCurrentUser( (ClaimsIdentity)filterContext.HttpContext.User.Identity); filterContext.Principal = user; base.OnAuthorization(filterContext); } 这只是在STS失败之前多次调用STS.我甚至尝试在分配之后交换到两者之后调用基数.没运气. 在之前的版本之前,我还尝试向控件添加AuthorizeFilter,但这没有帮助: http://pratapreddypilaka.blogspot.in/2012/03/custom-filters-in-mvc-authorization.html 解决方法
找到答案,YESSS!
所以,我找到了这个链接:http://brockallen.com/2013/01/17/adding-custom-roles-to-windows-roles-in-asp-net-using-claims/ 从那里,我猜到了我的方式,很好:) 以下是我所做的基础知识: 我最终覆盖了Controller的OnAuthentication方法,但仍然确保调用基础.我是在扩展课程中完成的.这是概念: public class AdfsController : Controller { //Some code for adding the AppUserManager (used Unity) protected override void OnAuthentication( System.Web.Mvc.Filters.AuthenticationContext filterContext) { base.OnAuthentication(filterContext); //Private method to set the Principal _setCurrentUser(filterContext.Principal); } private void _setCurrentUser(IPrincipal principal) { //Put code to find to use your ApplicationUserManager or //dbContext. roles is a string array foreach(var role in roles) { ((ClaimsIdentity)((ClaimsPrincipal)principal).Identity) .AddClaim(new Claim(ClaimTypes.Role,role)); } } } 在Controller中,您现在可以添加以下内容: public class HomeController : AdfsController { //I used a magic string for demo,but store these in my globals class [Authorize(Roles = "CoolRole")] public ActionResult Index() { return View(); } } 我通过检查分配给当前用户的角色对此进行了测试,这是有效的!然后我将角色更改为“拒绝”,用户未分配;我收到了401 Unauthorized.所以,是的,非常酷! 我知道我就是那个问这个问题的人,但希望有人觉得这很有帮助,并节省了一些时间. ?干杯! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – MVC3重定向到ajax调用后的动作
- asp.net-core-2.1 – 如何在asp.net Core 2.1.1中为Identit
- 按月分组的ASP.NET组数据
- asp.net – IIS中的自定义虚拟路径提供程序
- asp.net-mvc-3 – ASP.NET MVC3 Razor:如何遵循模块化架构
- asp.net-mvc-3 – 返回JsonFile的MVC3控制器
- asp.net – VS2015为Class Library添加引用
- asp.net-mvc – 想从mvc使用index.html
- asp.net-mvc – ASP.NET MVC 3 Treeview
- asp.net – 在Windows 2008 R2 SP1上,asmx的JQuery失败