asp.net-mvc - 将角色添加到 ADFS IPrincipal

标签 asp.net-mvc adfs ws-federation

几天来我一直在寻找这个问题的答案,但我没有找到任何成功。我会发布链接,但它可能会占用整个页面。

所以这就是我所拥有的......

我有一个 MVC 应用程序,它使用 WC-Federation 协议(protocol)。我已经能够配置应用程序,以便它对用户进行身份验证,并从 ADFS 返回声明。这很完美。我还可以毫无问题地提取所有 claim 。但我是在 Controller 中的一项操作中执行此操作的。

这就是我想要做的......

我想使用 ADFS 对用户进行身份验证,但我想使用自己的内部角色来授权用户访问特定 Controller (例如 [Authorize(Roles = "CoolRole")] )。我希望能够做到这一点,因为我已经有一个使用 OAuth 2.0 的 Web API,以及一个用于管理用户和角色(内部和外部用户)的后端 SQL Server 数据库。我现在想要一个允许内部用户的安全门户通过单点登录体验访问数据。看着Controller模型,我注意到有一些与身份验证过程相关的属性( OnAuthenticationOnAuthenticationChallenge )和一个与授权过程相关的属性( 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。我什至尝试在分配后交换到在两者中调用基础之后。没运气。

在之前的之前,我还尝试将 AuthorizeFilter 添加到控件中,但这没有帮助:

http://pratapreddypilaka.blogspot.in/2012/03/custom-filters-in-mvc-authorization.html

最佳答案

我找到了这个链接: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 - 将角色添加到 ADFS IPrincipal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30131878/

相关文章:

c# - ASP.NET MVC - 默认范围验证错误消息未被覆盖

javascript - 如何获取特别选择的下拉值

asp.net-mvc - "the project type is not supported by this installation"错误

asp.net - IDX10214 : Audience validation failed. ADFS.Net

c# - 联合身份 token 复制字符以关闭标签(cookie)(使用 Thinktecture Identity Server)

asp.net-mvc - Linq to Entity - 插入记录

asp.net-web-api - 身份验证/授权 MVC 5 和 Web API - Katana/Owin

c# - 如何设置有效的本地ADFS URI?

c# - AspNet Core 1.0.0中如何获取IOwinContext