A simple instance method for ASP.net Forms authentication _ Practical Tips

Source: Internet
Author: User
Tags instance method
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:

<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>
Related Article

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.