根据 Active Directory 对 ASP.NET MVC 应用程序的用户进行身份验证的高级步骤是什么?
我猜想是这样的:
- 修改 web.config 以使用 Windows 身份验证
- 配置 web.config 以使用 ActiveDirectoryMembershipProvider
- 配置 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 应用程序,一切都会为您处理
如果您想手动执行,则需要更改:
- 启用 Windows 身份验证
- 禁用匿名身份验证
有关在 IIS7/8 和 IISExpress 上执行此操作的详细信息:
IIS 7 & IIS 8
- Open IIS Manager and navigate to your website.
- In Features View, double-click Authentication.
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.
In the Actions pane, click Enable to use Windows authentication.
- On the Authentication page, select Anonymous authentication.
- In the Actions pane, click Disable to disable anonymous authentication.
IIS Express
- Right click on the project in Visual Studio and select Use IIS Express.
- Click on your project in the Solution Explorer to select the project.
- If the Properties pane is not open, open it (F4).
- 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
就像我一样:
这里有一些可以帮助您入门的内容
关于asp.net-mvc - 配置 ASP.NET MVC 以针对 AD 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10279140/