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");
}