Business System implementation Remember password and auto login function

Source: Internet
Author: User
Tags to domain

The company's business system was originally domain-controlled, users do not need to login to directly access the system. Of course, although not manually logged in, the system itself will read the computer's user name is the login ID, the system operation rights and records are also in the computer name. In the near time, because the system to lead to the cloud, that is, not subject to domain control, it is necessary to each have an account and password on hand, this and the general business system is no different. However, due to the user's previous habits are not logged in, and the number of open close every day more. OK, the general system login will have a password to remember the function, but this also can not meet the needs of users, then we give users more than one automatic login function, similar to QQ, I last visited the automatic login function, and then visit again will not have to login.

Take a look at the original code to read the domain user.

String strname = httpcontext.current.user.identity.name;//Gets the user with the domain name        string name = HttpContext.Current.User.Identity.Name.Substring (HttpContext.Current.User.Identity.Name.IndexOf ("\ \") + 1, Httpcontext.current.user.identity.name.length-httpcontext.current.user.identity.name.indexof ("\ \")-1);

A lot of children's shoes are getting empty because the Web. config is not set up. You must set the Mode property to Windows in the configuration file <authentication mode= "Windows" > the location in <configuration><system.web>.

OK, here's how to implement our remember Password and auto sign-in feature. Let's show you the login screen.

Let's talk about specific functions.

1, when the user tick remember password, the next time to open the login page automatically populate the user name and password, the user just click the login button can log in, of course, you can also delete the auto-filled password to re-enter, or you use another user password login line.
2, when the user check the automatic login, remember that the password will be automatically checked, because only remember the password will have automatic login, this is reasonable existence. The next time the user opens the login page, it will automatically verify the jump to the page after verification, no longer requires the user to click the login button.

First look at the front-end of the brief code, simple point to the need to use the control and JS paste out, the other layout is not posted out. JS Simple Explanation

<form runat= "Server" ><input type= "hidden" id= "Hidpass" runat= "Server"/><input type= "text" runat= " Server "id=" Txtloginname "/><asp:textbox runat=" Server "id=" Txtpassword "class=" Textwidthheigh "TextMode=" Password "></asp:textbox><input type=" checkbox "runat=" Server "value=" Remember Password "id=" Chkremember "onclick=" Checkremember () "/><input type=" checkbox "runat=" Server "value=" Auto Login "id=" Chklogin "onclick=" Checklogin () "/ ><asp:button runat= "Server" id= "Btnlogin" onclick= "Btnlogin_click"/><input type= "button" id= "BtnClear" Onclick= "Clear ()"/></form> <script type= "Text/javascript" language= "javascript" >//page load user name input box get focus
document.getElementById ("Txtloginname"). focus (); function Clear () {//user clicks Cancel, clears user name and user password
document.getElementById ("Txtloginname"). Value = ""; document.getElementById ("Txtpassword"). Value = ""; } function Checklogin () {//user check automatic login, remember password also tick on the
var remember = document.getElementById ("Chkremember"); Remember.checked = true; } function Checkremember () {var remenber = document.getElementById ("Chkremember"); var login = document.getElementById ("Chklogin");        if (remenber.checked = = False) {login.checked = false; When the user removes the remember password, it also removes the automatic login
}} </script>

protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack) {
Access to the client's cookies, two cookies, one login name, one password
HttpCookie Loginnamecookie = request.cookies["Bic_loginname"]; HttpCookie Loginpasscookie = request.cookies["Bic_pass"]; if (Loginnamecookie! = null) {//Login's cookies are not empty, populate the login name
Txtloginname.value = Loginnamecookie.value; if (Loginpasscookie! = null) {//password cookies are not empty, fill in the Password box and the Hidden password box, and of course our password is encrypted before it is saved to the cookie s go, as for the role of the Tibetan box will be seen after
THIS.TXTPASSWORD.ATTRIBUTES.ADD ("Value", loginpasscookie.value+ ""); Hidpass.value = Loginpasscookie.value + "";//assigns a value to the hidden control chkremember.checked = true; }//Get checked for automatic login cookies
HttpCookie Login = request.cookies["Bic_loginauto"]; When the user clicks out of the system
if (request["opflag"] = = "Exit") {THIS.TXTPASSWORD.ATTRIBUTES.ADD ("value", "");//Remove the password chkremember.checked = false;//Remember password removed HttpCookie Loginnamecookie = request.cookies["B Ic_loginname "];//Get Login Cookies
HttpCookie Loginpasscookie = request.cookies["Bic_pass"];//get password Cookies
if (Loginnamecookie! = null) {//Set the cookie time to-2 is equivalent to deleting the cookie
Loginnamecookie.expires = DateTime.Now.AddDays (-2); Response.Cookies.Set (Loginnamecookie); } if (Loginpasscookie! = null) {//password cookies are also deleted
Loginpasscookie.expires = DateTime.Now.AddDays (-2); Response.Cookies.Set (Loginpasscookie); }//Automatic login Cookies are also the same
HttpCookie login = request.cookies["Bic_loginauto"]; if (login! = null) {login. Expires = DateTime.Now.AddDays (-2); Response.Cookies.Set (login); }} else//when the user opens the login screen
{//Automatic login Cookies are not empty, user name is not empty, hidden box password is not empty
if (Login! = NULL && Txtloginname.value! = "" && hidpass.value! = "") { Sysuser user = new Sysuser (); User. login_name = Txtloginname.value; User. Login_pass = Hidpass.value; int i = sysuserbll.login (user);//Authentication Login
if (i > 0) {//successful login jump to Default.aspx page
page.session["login_name"] = user. login_name; httpcontext.current.session["display_name"] = sysuserbll.getdisplaynamebyname (user. login_name); Response.Write ("<script>window.location=" default.aspx ";</script>"); Response.End (); } } } }
}
Click the Login button event
protected void Btnlogin_click (object sender, EventArgs e)
{
Determines whether the empty
if (TxtLoginName.Value.Trim () = "" && txtPassWord.Text.Trim ()! = "" ")
{
Sysuser user = new Sysuser ();
User. Login_name = TxtLoginName.Value.Trim ();
User. Login_pass = Commonhelper.md5encipher (TxtPassWord.Text.Trim ());//MD5 encryption
HttpCookie Loginnamecookie = request.cookies["Bic_loginname"];
HttpCookie Loginpasscookie = request.cookies["Bic_pass"];
if (Loginnamecookie! = null)//If it is remembering the password condition
{
if (txtLoginName.Value.Trim () = = LoginNameCookie.Value.Trim ())//read to the cookie save user name and text box user name is the same, prevent users to change
{
if (Loginpasscookie! = null)
{
if (txtPassWord.Text.Trim () = = LoginPassCookie.Value.Trim ())//cookies read the same password as the text box
{
User. Login_pass = TxtPassWord.Text.Trim ();
}

}
}
}
int i = sysuserbll.login (user);//Authentication Login
if (i > 0)
{
if (chkremember.checked = = true)//Remember Password
{
HttpCookie Loginnamecookie = new HttpCookie ("Bic_loginname", user. login_name);
HttpCookie Loginpasscookie = new HttpCookie ("Bic_pass", user. Login_pass);
Loginpasscookie.expires = DateTime.Now.AddDays (1);
Loginnamecookie.expires = DateTime.Now.AddDays (1);
RESPONSE.COOKIES.ADD (Loginnamecookie);
RESPONSE.COOKIES.ADD (Loginpasscookie);
if (chklogin.checked = = true)//Auto Login
{
HttpCookie Login = new HttpCookie ("Bic_loginauto", "true");
Login.expires = DateTime.Now.AddDays (1);
RESPONSE.COOKIES.ADD (Login);
}
Else
{
HttpCookie Login = request.cookies["Bic_loginauto"];
if (Login! = null)
{
Login.expires = DateTime.Now.AddDays (-2);
Response.Cookies.Set (Login);
}
}
}
Else//didn't choose to remember the password.
{
HttpCookie Loginnamecookie = request.cookies["Bic_loginname"];
HttpCookie Loginpasscookie = request.cookies["Bic_pass"];
if (Loginnamecookie! = null)
{
Loginnamecookie.expires = DateTime.Now.AddDays (-2);
Response.Cookies.Set (Loginnamecookie);
}
if (Loginpasscookie! = null)
{
Loginpasscookie.expires = DateTime.Now.AddDays (-2);
Response.Cookies.Set (Loginpasscookie);
}
}
page.session["login_name"] = user. login_name;
httpcontext.current.session["display_name"] = sysuserbll.getdisplaynamebyname (user. login_name);
Response.Write ("<script>window.location=" default.aspx ";</script>");
}
Else
{
Response.Write ("<script>alert (' username or password is wrong! '); window.location= ' login.aspx ';</script> ');
}
}
Else
{
Response.Write ("<script>alert" ("Please enter your account number and password!"); window.location= ' login.aspx ';</script> ');
}
}


The above comments are just some of my personal ideas and understanding, if there is not correct, but also hope that Daniel guidance ah. If you feel that the article is helpful to you, please support me, your support will be my motivation to write a blog.

Turn the----http://www.16aspx.com/Article/3646

(turn) Business system implementation Remember password and auto login function

Related Article

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.