Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Authenticate users against Active Directory, but require username and password to be inputted

I'm developing a MVC3 application that will require a user to be authenticated against an AD. I know that there is the option in MVC3 to create an Intranet Application that automatically authenticates a user against an AD, but it uses Windows Authentication and automatically logs them on. This application may be accessed on 'Open' workstations where the user will need to enter their Domain Username and Password. Any examples or online tutorial would be great. An example project would be exceptional.

like image 486
munchrall Avatar asked May 27 '11 04:05

munchrall


1 Answers

You can use the standard Internet application template with forms authentication and insert an ActiveDirectoryMembershipProvider into the web.config:

<connectionStrings>     <add name="ADConnectionString" connectionString="LDAP://YOUR_AD_CONN_STRING" /> </connectionStrings>  <system.web>     <authentication mode="Forms">         <forms name=".ADAuthCookie" loginUrl="~/Account/LogOn"                timeout="15" slidingExpiration="false" protection="All" />     </authentication>     <membership defaultProvider="MY_ADMembershipProvider">         <providers>             <clear />             <add name="MY_ADMembershipProvider"                   type="System.Web.Security.ActiveDirectoryMembershipProvider"                   connectionStringName="ADConnectionString"                  attributeMapUsername="sAMAccountName" />         </providers>     </membership> </system.web> 

In this way you get the Internet application template login form, and it validates against AD for you.

Then it's just a matter of some AccountController cleanup to remove reset password/change password/register functionality leaving just Login.

like image 196
Khepri Avatar answered Nov 03 '22 14:11

Khepri