asp.net-mvc - 配置 ASP.NET MVC 以针对 AD 进行身份验证

标签 asp.net-mvc active-directory

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

我猜想是这样的:

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

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

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

最佳答案

表单例份验证

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

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

并添加成员(member)提供者以使用此连接:

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

您需要使用用户名@域来成功验证用户身份。

这里有一些可以帮助您入门的内容

<小时/>

Windows 身份验证

如果您开始新的项目,您可以随时从模板中选择Intranet 应用程序,一切都会为您处理

enter image description here

如果您想手动执行,则需要更改:

  1. 启用 Windows 身份验证
  2. 禁用匿名身份验证

有关在 IIS7/8 和 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".

在你的web.config中有类似的内容

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

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

就是这样!

现在,当您需要用户身份时,只需调用

@User.Identity.Name

这将向您显示 Domain\Username 就像我一样:

enter image description here

这里有一些可以帮助您入门的内容

关于asp.net-mvc - 配置 ASP.NET MVC 以针对 AD 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10279140/

相关文章:

asp.net-mvc - 将ViewData传递给RenderPartial

javascript - ASP.NET MVC 上使用 JavaScriptSerializer 进行 Unicode 字符转义取决于配置?

html - MVC 形式 : align Actionlink and button next to each other on same vertical alignment

java - 使用 Java 进行 LDAP 身份验证

c# - 与 Active Directory 集成的 .NET 应用程序的单点登录

c++ - 如何获取某个域用户的访问 token ?

c# - 使用泛型类型 'System.Collections.Generic.List<T>' 需要 1 个类型参数

active-directory - Microsoft AD GUID 不匹配

c++ - LDAP 的 ldap_search_s() 在 Windows Active Directory 上失败

javascript - 从带有捆绑的 View 中将 Javascript 添加到 ASP.NET/MVC 网页的标题部分?