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

发布于 2024-12-29 11:43:01 字数 2361 浏览 1 评论 0原文

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

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

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

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

除了在活动目录中定义的 你认为这是解决我的问题的最佳方案。除了 Windows 身份验证之外,我是否真的必须使用表单身份验证来实现复杂的混合模式身份验证?

使用的技术:

  • MS SQL Server 2008
  • MS VS 2010
  • ASP.NET MVC 3 -
  • 的 Razor View Engine Telerik 扩展
  • Windows Server 2008 上 ASP.NET MVC 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;
        }
    }
}

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

它有效!是的!

after a lot of googling and reading several solutions on how to manage mixed mode authentication in ASP.NET apps, I still have no fitting solution for my problem.

I've got to implement an intranet application for a bunch of different user groups. Until now i've used windows authenthication which was very simple to implement. My problems arise when it comes to authorizing usergroups for special application functionalities.

Using [Authorize(Users = "DOMAIN\\USER")] works great but due to that i have no access to the active directory managament, it is impossible to me to configure rolemanagement in the way I need it for my application.

What I'd like to do is defining custom roles and memberships in addition to the ones that are defined within the active directory (is such an extension possible? e.g. by implementing an own membershipprovider?).

What do you think is the best solution for my problem. Do I really have to implement a complex mixed mode authentication with forms authentication in addition to windows authentication?

Used Technologies:

  • MS SQL Server 2008
  • MS VS 2010
  • ASP.NET MVC 3 - Razor View Engine
  • Telerik Extensions for ASP.NET MVC
  • IIS 7 on Windows Server 2008

EDIT (final solution thanks to the help of dougajmcdonald):

After pointing me to use a custom IPrincipal implementation I've found some solutions here and here. Putting everything together I came to the following solution:

1.Create a custom principal implementation:

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.Integrate my custom principal implementation into the application through extending the "Global.asax.cs" file:

    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.Use my custom roles for authorization in my application

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

        return View();
    }
}

It works!!! yeah!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2025-01-05 11:43:01

我不确定这是否仍然适用于 MVC,但在 Webforms 中,执行此操作的一种方法如下:

  1. 创建一个新的 IPrincipal 实现,可能扩展 WindowsPrincipal
  2. 在此类中,为其提供角色集合(您自己的自定义角色)
  3. Populate这些角色,也许可以从数据库中获取。
  4. 如果提供的角色从基本调用 (WindowsAuthentication/Role) 或您自己的自定义角色集合中为 true,则重写 IsInRole 以返回 true。

这样您仍然可以连接到Principal.IsInRole("MyRole") 以及主体[PrincipalPermission()] 注释。

希望有帮助。

编辑回答q's:

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

我相信授权会在稍后的日期加入到PrincipalPermission中,但我不太确定何时/为何会出现差异,我担心:( - 抱歉!

I'm not sure if this still applies in MVC, but in Webforms one way to do this would be as follows:

  1. Create a new IPrincipal implementation perhaps extending WindowsPrincipal
  2. In this class, give it a collection of roles (your own custom roles)
  3. Populate those roles, by perhaps getting them from the DB.
  4. Override IsInRole to return true if the role provided is EITHER true from the base call (WindowsAuthentication/Role) OR from your own custom role collection.

This way you can still hook into Principal.IsInRole("MyRole") and also the principal [PrincipalPermission()] annotation.

Hope it helps.

EDIT in answer to q's:

To integrate the principal into the authorisation you need to write your own method for OnAuthenticate in the global.asax for the type of authentication, so I would guess for you, something like this:

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

I believe Authorize came in at a later date to the PrincipalPermission, but I'm not too sure as to when/why of the differences I'm afraid :( - sorry!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文