几天来,我一直在寻找这个问题的答案,但我没有找到任何成功的答案。我会张贴的链接,但它可能会占用整个页面。
这就是我所拥有的..。
我有一个MVC应用程序,它使用协议.我已经能够配置应用程序,以便它对用户进行身份验证,并返回来自ADFS的声明。这是完美的。我也可以在没有问题的情况下提取所有的索赔。但是我是在控制器中的一个动作中这样做的。
我想做的就是..。
我想使用ADFS对用户进行身份验证,但我希望使用自己的内部角色来授权用户访问特定的控制器(例如[Authorize(Roles = "CoolRole")])。我希望能够做到这一点,因为我已经有了一个使用OAuth 2.0的Web,它有一个后端Server数据库来管理用户和角色(内部和外部用户)。我现在想要一个安全的门户,允许内部用户以单点登录的方式访问数据。查看Controller模型,我注意到与身份验证过程(OnAuthentication、OnAuthenticationChallenge)和授权过程(OnAuthorization)相关的一些属性。
我不一定需要这些代码,但我觉得我已经完全崩溃了,我需要被指引到正确的方向。
更新
我试过这个:
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
发布于 2015-05-09 00:07:37
我找到了这个链接: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未经授权。
发布于 2015-05-08 21:00:52
ADFS是Azure中的身份验证/令牌服务。为了启用基于角色的身份验证,您可以使用Azure RBAC (基于角色的访问控制)服务来增加从ADFS返回的声明,并将您从RBAC获得的角色添加到令牌中,并在API中使用相同的令牌从而锁定或使用该增强令牌保护后端.以下是RBAC的参考:
http://azure.microsoft.com/en-in/documentation/articles/role-based-access-control-configure/
https://stackoverflow.com/questions/30131878
复制相似问题