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

2024-04-08

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

我必须为一堆不同的用户组实现一个 Intranet 应用程序。到目前为止,我一直使用 Windows 身份验证,它的实现非常简单。当涉及到授权用户组使用特殊应用程序功能时,我遇到了问题。

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

除了在活动目录中定义的角色和成员资格之外,我想做的是定义自定义角色和成员资格(这样的扩展可能吗?例如,通过实现自己的membershipprovider?)。

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

使用的技术:

  • 微软 SQL Server 2008
  • 女士VS 2010
  • ASP.NET MVC 3 - Razor 视图引擎
  • ASP.NET MVC 的 Telerik 扩展
  • Windows Server 2008 上的 IIS 7

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

在指示我使用自定义 IPrincipal 实现后,我找到了一些解决方案here http://smehrozalam.wordpress.com/2009/01/01/using-customprincipal-with-forms-authentication-in-aspnet/ and here http://jbaurle.wordpress.com/2007/07/24/creating-custom-windows-authentication-roles-in-asp-net-2/。将所有内容放在一起,我得出以下解决方案:

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;
        }
    }
}

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;
        }
    }

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

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

        return View();
    }
}

有用!!!是的!


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

  1. 创建一个新的 IPrincipal 实现,或许可以扩展 WindowsPrincipal
  2. 在此类中,为其提供角色集合(您自己的自定义角色)
  3. 填充这些角色,也许可以从数据库中获取它们。
  4. 如果提供的角色从基本调用 (WindowsAuthentication/Role) 或您自己的自定义角色集合中为 true,则重写 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;    
    }
}

我相信 Authorize 会在稍后的日期加入到 PrimaryPermission 中,但我不太确定何时/为何会出现差异,恐怕:( - 抱歉!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ASP.NET MVC 3 应用程序中扩展 Windows 身份验证 的相关文章

随机推荐