在ASP.NET MVC 3应用程序中扩展Windows身份验证

Wow*_*owe 14 iis asp.net-mvc asp.net-membership

经过大量的谷歌搜索和阅读有关如何在ASP.NET应用程序中管理混合模式身份验证的几个解决方案,我仍然没有适合我的问题的解决方案.

我必须为一堆不同的用户组实现一个Intranet应用程序.到目前为止,我已经使用了很容易实现的windows authentication.在授权用户组获取特殊应用程序功能时,我的问题就出现了.

使用[Authorize(Users = "DOMAIN\\USER")]效果很好,但由于我无法访问活动目录管理,因此我无法按照我的应用程序的需要配置角色管理.

我想做的是定义自定义角色和成员资格以及在活动目录中定义的那些(这样的扩展是否可能?例如通过实现自己的成员提供者?).

你认为什么是我的问题的最佳解决方案.除了Windows身份验证之外,我是否真的必须使用表单身份验证实现复杂的混合模式身份验证?

二手技术:

  • MS SQL Server 2008
  • MS VS 2010
  • ASP.NET MVC 3 - Razor View Engine
  • ASP.NET MVC的Telerik扩展
  • Windows Server 2008上的IIS 7

编辑(感谢dougajmcdonald帮助的最终解决方案):

在指向我使用自定义IPrincipal实现后,我在这里这里找到了一些解决方案.将所有内容放在一起我得出以下解决方案:

1.创建自定义主体实现:

public class MyPrincipal: WindowsPrincipal
{
    List<string> _roles;

    public MyPrincipal(WindowsIdentity identity) : base(identity) {
        // fill roles with a sample string just to test if it works
        _roles = new List<string>{"someTestRole"}; 
        // TODO: Get roles for the identity out of a custom DB table
    }

    public override bool IsInRole(string role)
    {
        if (base.IsInRole(role) || _roles.Contains(role))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2.通过扩展"Global.asax.cs"文件将我的自定义主体实现集成到应用程序中:

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            WindowsIdentity wi = (WindowsIdentity)HttpContext.Current.User.Identity;
            MyPrincipal mp = new MyPrincipal(wi);
            HttpContext.Current.User = mp;
        }
    }
Run Code Online (Sandbox Code Playgroud)

3.在我的应用程序中使用我的自定义角色进行授权

public class HomeController : Controller
{
    [Authorize(Roles= "someTestRole")]
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

有用!!!是啊!

dou*_*ald 8

我不确定这是否仍然适用于MVC,但在Webforms中,一种方法是这样做:

  1. 创建一个新的IPrincipal实现,可能会扩展WindowsPrincipal
  2. 在这个类中,给它一组角色(你自己的自定义角色)
  3. 通过从DB中获取角色来填充这些角色.
  4. 如果从基本调用(WindowsAuthentication/Role)或您自己的自定义角色集合中提供的角色为EITHER,则覆盖IsInRole以返回true.

这样你仍然可以挂钩到Principal.IsInRole("MyRole")以及主体[PrincipalPermission()]注释.

希望能帮助到你.

编辑回答q:

要将主体集成到授权中,您需要在global.asax中为身份验证类型编写自己的OnAuthenticate方法,所以我想你会这样:

void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
{
    // ensure we have a name and made it through authentication
    if (e.Identity != null && e.Identity.IsAuthenticated)
    {
        //create your principal, pass in the identity so you know what permissions are tied to
        MyCustomePrincipal opPrincipal = new MyCustomePrincipal(e.Identity);            
        //assign your principal to the HttpContext.Current.User, or perhaps Thread.Current
        HttpContext.Current.User = opPrincipal;    
    }
}
Run Code Online (Sandbox Code Playgroud)

我相信授权会在以后的日期进入PrincipalPermission,但我不太确定何时/为什么我害怕差异:( - 对不起!