配置 ASP.NET MVC 以针对 AD 进行身份验证 [英] Configure ASP.NET MVC for authentication against AD

查看:32
本文介绍了配置 ASP.NET MVC 以针对 AD 进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对 Active Directory 对 ASP.NET MVC 应用程序的用户进行身份验证的高级步骤是什么?

我认为是这样的:

  1. 修改 web.config 以使用 Windows 身份验证
  2. 配置 web.config 以使用 ActiveDirectoryMembershipProvider
  3. 配置 web.config 以使用在 AD 中查找的自定义 RoleProvider

以上看起来是否合理,如果是,我应该将有效的用户检测逻辑放在哪里?

就我而言,有效用户是特定 AD 域中的某个人.

解决方案

Forms Authentication

您可以使用普通表单身份验证针对 Active Directory 对用户进行身份验证,为此您只需要 AD 连接字符串:

<add name="ADConn" connectionString="LDAP://YourConnection"/></connectionStrings>

并添加会员提供程序以使用此连接:

<提供者><add name="ADMembership"type="System.Web.Security.ActiveDirectoryMembershipProvider,系统.Web,版本=2.0.0.0,文化=中立,PublicToken=b03f5f7f11d50a3a"connectionStringName="ADConn"connectionUsername="域/用户"connectionPassword="pwd"/></提供者></会员资格>

您需要使用 username@domain 来成功验证用户身份.

这里有一些东西可以让你开始

What are the high level steps to authenticate users of an ASP.NET MVC application against Active Directory?

I presume something like:

  1. Modify web.config to use Windows authentication
  2. Configure web.config to use the ActiveDirectoryMembershipProvider
  3. Configure the web.config to use a custom RoleProvider that looks in AD

Does the above look sensible, and if so, where do I put the valid user detection logic?

In my case a valid user is someone on a specific AD domain.

解决方案

Forms Authentication

You can use the normal forms authentication to authenticate a user against an Active Directory, for that you just need you AD connection string:

<connectionStrings>
  <add name="ADConn" connectionString="LDAP://YourConnection" />
</connectionStrings>

and add the Membership Provider to use this connection:

<membership defaultProvider="ADMembership">
  <providers>
    <add name="ADMembership"
         type="System.Web.Security.ActiveDirectoryMembershipProvider,
               System.Web,
               Version=2.0.0.0, 
               Culture=neutral,
               PublicToken=b03f5f7f11d50a3a"
         connectionStringName="ADConn"
         connectionUsername="domain/user"
         connectionPassword="pwd" />
  </providers>
</membership>

you will need to use username@domain to successfully authenticate the user.

Here is something to get you started


Windows Authentication

If you start your project new, you can always select Intranet application from the template and all is taken care for you

If you want to do it manually, you need to change:

  1. Enable Windows Authentication
  2. Disable Anonymous authentication

for detailed info on doing this on IIS7/8 and IISExpress:

IIS 7 & IIS 8

  1. Open IIS Manager and navigate to your website.
  2. In Features View, double-click Authentication.
  3. On the Authentication page, select Windows authentication. If Windows authentication is not an option, you'll need to make sure Windows authentication is installed on the server.

    To enable Windows authentication on Windows: a) In Control Panel open "Programs and Features". b) Select "Turn Windows features on or off". c) Navigate to Internet Information Services > World Wide Web Services > Security and make sure the Windows authentication node is checked.

    To enable Windows authentication on Windows Server: a) In Server Manager, select Web Server (IIS) and click Add Role Services b) Navigate to Web Server > Security and make sure the Windows authentication node is checked.

  4. In the Actions pane, click Enable to use Windows authentication.

  5. On the Authentication page, select Anonymous authentication.
  6. In the Actions pane, click Disable to disable anonymous authentication.

IIS Express

  1. Right click on the project in Visual Studio and select Use IIS Express.
  2. Click on your project in the Solution Explorer to select the project.
  3. If the Properties pane is not open, open it (F4).
  4. In the Properties pane for your project: a) Set "Anonymous Authentication" to "Disabled". b) Set "Windows Authentication" to "Enabled".

In your web.config have something like

<system.web>
  <authentication mode="Windows" />

  <authorization>
    <deny users="?" />
  </authorization>
</system.web>

and that's it!

Now, when you want the user identity, just call

@User.Identity.Name

and this will show you the DomainUsername like for me :

Here is something to get you started

这篇关于配置 ASP.NET MVC 以针对 AD 进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆