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 current login username.
Web.config
Copy Code code as follows:
<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
Copy Code code as follows:
<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
Copy Code code as follows:
<TITLE> home </title>
<script lanugage= "C #" runat= "Server"
private void Page_Load (object sender, EventArgs e)
{
if (request.isauthenticated)
& nbsp Response.Redirect ("default.aspx");
}
void Btnlogin_click (Object sender, EventArgs e)
{
if (txtUsername.Text!= null && txtUsername.Text!= String.Empty && txtpassword.text!= null && txtpassword.text!= string.empty)
&nb sp; FormsAuthentication.RedirectFromLoginPage (txtUsername.Text, true);
Else
lblerror.text = "Bad username/password";
</script>
<body>
<form method= "POST" runat= "server"
<asp:label id= "lblusername" runat= "Server" text= "User name:"/>
<asp:textbox id= "txtUserName" runat= "Server"/>
<br>
<br>
&NBSP;<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>
& Nbsp;<asp:label id= "Lblerror" forecolor= "Red" runat= "Server"/>
</body>