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

Asp.net mvc 在ASP.NET MVC 3应用程序中扩展Windows身份验证,asp.net-mvc,iis,asp.net-membership,Asp.net Mvc,Iis,Asp.net Membership,在谷歌搜索了很多关于如何在ASP.NET应用程序中管理混合模式身份验证的解决方案并阅读了很多之后,我仍然没有适合我的问题的解决方案 我必须为许多不同的用户组实现一个intranet应用程序。到目前为止,我一直使用windows身份验证,它的实现非常简单。我的问题出现在为特殊应用程序功能授权用户组时 使用[Authorize(Users=“DOMAIN\\USER”)]效果很好,但由于我无法访问active directory管理,因此无法按照我的应用程序所需的方式配置角色管理 除了在active

在谷歌搜索了很多关于如何在ASP.NET应用程序中管理混合模式身份验证的解决方案并阅读了很多之后,我仍然没有适合我的问题的解决方案

我必须为许多不同的用户组实现一个intranet应用程序。到目前为止,我一直使用windows身份验证,它的实现非常简单。我的问题出现在为特殊应用程序功能授权用户组时

使用
[Authorize(Users=“DOMAIN\\USER”)]
效果很好,但由于我无法访问active directory管理,因此无法按照我的应用程序所需的方式配置角色管理

除了在active directory中定义的角色和成员身份之外,我想做的是定义自定义角色和成员身份(这样的扩展是否可行?例如,通过实现自己的成员身份提供程序?)。

你认为解决我问题的最好办法是什么。除了windows身份验证之外,我真的必须使用窗体身份验证实现复杂的混合模式身份验证吗

使用的技术:

  • MS SQL Server 2008
  • MS VS 2010
  • ASP.NET MVC 3-Razor视图引擎
  • 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;
        }
    }
}
3.在我的应用程序中使用我的自定义角色进行授权

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

        return View();
    }
}

它工作!!!是啊

我不确定这是否仍然适用于MVC,但在Webforms中,实现这一点的一种方法如下:

  • 创建一个新的IPrincipal实现,可能扩展WindowsPrincipal
  • 在这个类中,为它提供一个角色集合(您自己的自定义角色)
  • 通过从数据库中获取这些角色来填充这些角色
  • 如果从基本调用(WindowsAuthentication/role)或从您自己的自定义角色集合提供的角色为true,则重写IsInRole以返回true
  • 这样,您仍然可以钩住Principal.IsInRole(“MyRole”)和Principal[PrincipalPermission()]注释

    希望能有帮助

    在回答问题时进行编辑:

    要将委托人集成到授权中,您需要在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;    
        }
    }
    

    我相信Authorize是在晚些时候进入PrincipalPermission的,但我不太确定什么时候/为什么会有这些差异,我担心:(-对不起!

    没问题,很高兴它起到了作用。我最近不得不做一些非常类似的事情!在我的情况下,我不想使用Windows Active Directory角色(因为我们的管理员不希望以这种方式管理事情),但我想确保如果他们决定使用它们,我们可以使用它们,因此,我们决定将它们绑定到IPrincipal方面,而不仅仅是创建一个用户帐户作为我们应用程序的内部类,并在其上设置角色。
    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;    
        }
    }