Authentication in asp.net

Source: Internet
Author: User
Authentication
in asp.net asp.net




I use forms-based validation, which is also the most common, I only write the summary, the source code is too long, but should not affect the understanding of the code.




Modification of
web.config:


<authentication mode= "Forms"/>


User's login authentication method,:


here are two input controls, one is USER_TB, used to enter the user, one is the PSW_TB, used to enter the password


private void Button1_Click (object sender, System.EventArgs e)


{


//User Login Verification


string ip= System.Web.HttpContext.Current.Request.UserHostAddress;


string USER_NAME=USER_TB. Text;


string USER_PSW=PSW_TB. Text;


User_name=user_name. Replace ("<", "<"). Replace (">", ">"). Replace ("", ""). Replace ("'", "" ");


USER_PSW=USER_PSW. Replace ("<", "<"). Replace (">", ">"). Replace ("", ""). Replace ("'", "" ");


if (user_name!= "" | | User_psw!= "")


{


SqlConnection Myconn=new SqlConnection ((string) configurationsettings.appsettings["ConnString");//Connection Database


MyConn. Open ()//Turn on


string validate_sql= "select * from Web_user where user_name= '" +user_name+ "' and User_psw= '" "";


SqlCommand validate_com=new SqlCommand (validate_sql,myconn);


SqlDataReader validate=validate_com. ExecuteReader ();


string temp= "";


while (validate. Read ())


{


session["user_name"]=user_name;


session["User_flag"]=validate["User_flag"]. ToString ();


temp= "yes";


}


Validate. Close ();


if (temp== "yes")


{


USER_TB. Text= "";


PSW_TB. Text= "";


System.Web.Security.FormsAuthentication.RedirectFromLoginPage (User_name,false);


Response.Redirect ("manage_index.aspx");





}


Else


{


Response.Write ("<script>alert your username or password is wrong!" ');</script> ");


return;


}


USER_TB. Text= "";


PSW_TB. Text= "";


myconn. Close ();


}





}


to determine whether the user has logged in:


private void Page_Load (object sender, System.EventArgs e)


{


//Here to determine if the user has landed


if (! System.Web.HttpContext.Current.User.Identity.IsAuthenticated)


{


Response.Write ("<script>alert") (' You did not login! '); History.back () </script> ");


}


string strUserName;


strusername=system.web.httpcontext.current.user.identity.name+ "<br>+ System.Web.HttpContext.Current.User.Identity.IsAuthenticated ";


Lbiusername.text=strusername;


//Place user code here to initialize page


}


User's exit:


private void LinkButton1_Click (object sender, System.EventArgs e)


{//Exit


session["user_name"]=null;


session["User_flag"]=null;


session.clear ();


System.Web.Security.FormsAuthentication.SignOut ();


Response.Redirect ("default.aspx");


}














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.