1. Configuring the Web. config file
< connectionStrings > < name= "adconnectionstring" connectionString= "ldap://dc1.xxx.com.cn:389/ DC=XXX,DC=COM,DC=CN "/> </connectionStrings>
<system.web> <Membershipdefaultprovider= "Admembershipprovider"> <providers> <Clear/> <Addname= "Admembershipprovider"type= "System.Web.Security.ActiveDirectoryMembershipProvider"connectionStringName= "Adconnectionstring"attributeMapUserName= "sAMAccountName" /> </providers> </Membership> </system.web>
2. Use the configured membership in your code to verify user login information
PublicActionResult Login (Loginmodel model,stringreturnUrl) { if(! This. Modelstate.isvalid) {return This. View (model); } if(Membership.ValidateUser (model. UserId, model. Password)) {MembershipUser user=Membership.getuser (model. USERID); //User Roleslist<string> Roles =Newlist<string>(); ... FormsAuthenticationTicket ticket=NewFormsAuthenticationTicket (1, model. UserId, DateTime.Now, DateTime.Now.AddDays ( -),//set the time to remember login (this is 30 days) true, String.Join (",", roles)); HttpCookie Cookies=NewHttpCookie (Formsauthentication.formscookiename, Formsauthentication.encrypt (ticket)); //set the expiration time of the cookie (this is 50)Cookies. Expires = DateTime.Now.AddYears ( -); RESPONSE.COOKIES.ADD (cookie); return This. Redirecttoaction ("Index","Home"); } This. Modelstate.addmodelerror (string. Empty,"The user name or password is incorrect. "); return This. View (model);}
Added to "MVC5" ASP. NET MVC Project notes Summary
"MVC5" Login with domain user + remember Me