简体   繁体   English

使用 AD 的 MVC3 授权

[英]MVC3 authorization using AD

Is it possible to authorise/deny users of an MVC3 application using AD?是否可以使用 AD 授权/拒绝 MVC3 应用程序的用户?

My app is secured using Windows authentication at the moment, but that means adding users to groups on the Win2007 server.我的应用程序目前使用 Windows 身份验证进行保护,但这意味着将用户添加到 Win2007 服务器上的组。

I'd like to change that so that users were allowed/denied access to the appliction/and controller actions/view based upon their AD roles instead, so they either auto-logged in (like Windows auth) or they get redirected to a "denied" page.我想更改它,以便允许/拒绝用户访问应用程序/和 controller 操作/查看基于他们的 AD 角色,因此他们要么自动登录(如 Windows 身份验证),要么被重定向到“拒绝”页面。

Any help very gratefully accepted...everything I find seems to be based upon Windows groups, or forms authentication.非常感激地接受任何帮助......我发现的一切似乎都基于 Windows 组或 forms 身份验证。

You could use the Roles property:您可以使用 Roles 属性:

[Authorize(Roles = @"SOMEDOMAIN\somegroup")]
public ActionResult Foo()
{
    ...
}

Here's a tutorial which explains the steps.这是一个解释这些步骤的教程

I'm using AD Groups for my intranet app.我正在为我的 Intranet 应用程序使用 AD 组。

<authentication mode="Windows" />
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
  <providers>
    <clear />
    <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
</roleManager>

then just added Authorization attributes to my controller actions that I needed to secure:然后只需将授权属性添加到我需要保护的 controller 操作中:

[Authorize(Roles = MyNamesspace.Constants.MANAGER_GROUP)]
public ActionResult Blah() {...

And in a view you can use User.IsInRole and the name of their AD/Windows group.在视图中,您可以使用User.IsInRole及其 AD/Windows 组的名称。

Or get a list of the roles the webserver sees from that user: System.Web.Security.Roles.GetRolesForUser();或者获取网络服务器从该用户看到的角色列表: System.Web.Security.Roles.GetRolesForUser();

Caveat: my server and my clients are all on the same domain.警告:我的服务器和我的客户端都在同一个域上。 this won't work if you need to do the same for web clients off site against your ActiveDirectory.如果您需要针对您的 ActiveDirectory 对 web 客户端在场外执行相同操作,这将不起作用。

Just use the Membership provider framework that comes built-in to Asp.net.只需使用 Asp.net 内置的成员资格提供程序框架。 You will find that there is already an ActiveDirectoryMembershipProvider out of the box, but you will have to implement the RoleProvider yourself, as membership can be defined different ways in different networks.您会发现已经有一个开箱即用的ActiveDirectoryMembershipProvider ,但是您必须自己实现RoleProvider ,因为在不同的网络中可以以不同的方式定义成员资格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM