asp.net when access to the default home default.aspx, will automatically jump to the Login.aspx page request login, random input username and password, click the "Login" button, will return to the home page, and display the user name of the current login.
Web.config
<configuration>
<system.web>
<compilation debug= "true"/>
<authentication mode= "Forms" >
<forms loginurl= "login.aspx" name = ". Aspxformsauth "/>
</authentication>
<authorization>
<deny users= "?" />
</authorization>
</system.web>
</configuration>
Default.aspx
<HTML>
<HEAD>
<TITLE> Home </TITLE>
<script language= "VB" runat= "Server" >
Sub Page_Load (Sender as Object, e as EventArgs)
Message.Text = String.Format ("Hello, {0}", Context.User.Identity.Name)
End Sub
Sub Btnsignout_click (Sender as Object, E as EventArgs)
FormsAuthentication.SignOut ()
Response.Redirect ("Login.aspx")
End Sub
</script>
</HEAD>
<BODY>
<asp:label id= "message" runat= "Server"/>
<br>
<form method= "POST" runat= "Server" >
<asp:button id= "Btnsignout" text= "Exit Login" runat= "server" onclick= "Btnsignout_click"/>
</form>
</BODY>
</HTML>
Login.aspx
<HEAD>
<TITLE> Home </TITLE>
<script lanugage= "C #" runat= "Server" >
private void Page_Load (object sender, EventArgs e)
{
if (request.isauthenticated)
Response.Redirect ("default.aspx");
}
void Btnlogin_click (Object sender, EventArgs e)
{
if (txtUsername.Text!= null && txtusername.text!= string.empty && txtpassword.text!= Null && TX Tpassword.text!= String.Empty)
FormsAuthentication.RedirectFromLoginPage (txtUsername.Text, true);
Else
Lblerror.text = "Wrong username/password";
}
</script>
</HEAD>
<BODY>
<form method= "POST" runat= "Server" >
<asp:label id= "Lblusername" runat= "server" text= "Username:"/>
<asp:textbox id= "txtUserName" runat= "Server"/>
<br>
<br>
<asp:label id= "Lblpassword" runat= "server" text= "Password:"/>
<asp:textbox id= "Txtpassword" runat= "server" textmode= "password"/>
<br>
<asp:button id= "Btnlogin" runat= "server" text= "login" onclick= "Btnlogin_click"/>
</form>
<br>
<asp:label id= "Lblerror" forecolor= "Red" runat= "Server"/>
</BODY>
</HTML>