"MVC5" Login with domain user + remember Me

Source: Internet
Author: User
Tags ticket connectionstrings

1. Configuring the Web. config file
    • Add a domain link string
< connectionStrings >    <  name= "adconnectionstring"  connectionString= "ldap://dc1.xxx.com.cn:389/ DC=XXX,DC=COM,DC=CN "/>  </connectionStrings>
    • Add Membership
<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.