General mode for. NET Logins

Source: Internet
Author: User
Tags httpcontext set cookie

Ideas:

1. Check Login:

private String Checklogin (HttpContext context) {    Model.tuser Model = (model.tuser) context. session["Usermodel"];    if (model = = null)    {        return "{\" msg\ ": \" 0\ ", \" msgbox\ ": \" is currently exiting \ "}";    }    else    {        return "{\" msg\ ": \" 1\ ", \" msgbox\ ": \" current login \ "}";}    

2. Click Login:

private String Login (HttpContext context) {BLL. TUser _blluser = new BLL.    TUser (); String loginname = context.    request.params["LoginName"]; String Password = context.    request.params["Password"]; String Issave = context. request.params["Issave"];//whether to save the password Model.tuser model_user = _blluser.    Getmodel ("userlogin=" + loginname + "'");    BOOL issuccess = false;    String cookiespassword = ""; Determine if the cookie exists if (httpcontext.current.request.cookies["password"]! = null) {Cookiespassword = Httpcontex t.current.request.cookies["Password"].        Value;        if (password = = Cookiespassword)///input and the same as the cookie password {//login successful issuccess = true; }}//Determine if the password entered and whether the database matches if (!issuccess) {if (password = = Model_user.        PassWord) {//Login succeeded issuccess = true;        } else {//Login failed issuccess = false; }} if (issuccess) {context. session["Usermodel"]= Model_user;    } else {//Login failed password not return "{\" msg\ ": \" 0\ ", \" msgbox\ ": \" password error \ "}";        }//To save, write cookie if (Issave = = "true") {HttpCookie cookielogin = new HttpCookie ("LoginName");        Cookielogin.expires = DateTime.Now.AddDays (10); Cookielogin.value = Model_user.        Userlogin;        HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookielogin);        HttpCookie Cookiepassword = new HttpCookie ("password");        Cookiepassword.expires = DateTime.Now.AddDays (10);        Cookiepassword.value = password;    HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookiepassword);        } else {//Set cookie expires, delete cookie HttpCookie cookielogin = new HttpCookie ("LoginName");        Cookielogin.expires = DateTime.Now.AddDays (-1);        HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookielogin);        HttpCookie Cookiepassword = new HttpCookie ("password");        Cookiepassword.expires = DateTime.Now.AddDays (-1); HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookiepassword); } return "{\" msg\ ": \" 1\ ", \" msgbox\ ": \" Login succeeded \ "}";}

3, the page refresh when the user saved account name and password and do not log in

private String Issaveuserlogin (HttpContext context) {///determine if the cookie exists if (httpcontext.current.request.cookies["log Inname "] = = NULL | | httpcontext.current.request.cookies["password"] = = null) {return "{\" msg\ ":-1, \" msgbox\ ": \" account not saved \ ", \" Loginna    Me\ ": \" \ ", \" password\ ": \" \ "}"; } else {string loginname = httpcontext.current.request.cookies["LoginName"].        Value; string password = httpcontext.current.request.cookies["Password"].        Value;        Free Landing cookielogin (context, loginname, password);    Return "{\" msg\ ": 1, \" msgbox\ ": \" account saved \ ", \" loginname\ ": \" "+ LoginName +" \ ", \" password\ ": \" "+ Password +" \ "}"; }}private void Cookielogin (HttpContext context, String Cookiesloginname, String cookiespassword) {Model.tuser Model_us    ER = new Model.tuser (); Bll. TUser _blluser = new BLL.    TUser ();    BOOL issuccess = false; Determine if the cookie exists if (!string. IsNullOrEmpty (cookiesloginname) &&!string. IsNullOrEmpty (Cookiespassword) {Model_user = _blluser.        Getmodel ("userlogin=" + cookiesloginname + "'"); if (Model_user.        PassWord = = Cookiespassword)//database and the same as the cookie password {//login successful issuccess = true; }} if (issuccess) {//Login successful context.    session["Usermodel"] = Model_user;    The else {//Login failed password is not//return "{\" msg\ ": \" 0\ ", \" msgbox\ ": \" password error \ "}"; }}

4. User exit-Clear cookies and session

private String UserExit (HttpContext context) {    //clear Cookie    //httpcontext.current.request.cookies.clear ();// Invalid    HttpCookie cookielogin = new HttpCookie ("LoginName");    Cookielogin.expires = DateTime.Now.AddDays ( -1);    HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (cookielogin);    HttpCookie Cookiepassword = new HttpCookie ("password");    Cookiepassword.expires = DateTime.Now.AddDays ( -1);    HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cookiepassword);    Context. session["Usermodel"] = null;    Context. Session.clear ();    Return "";}

General mode for. NET Logins

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.