A simple instance method of ASP.net Forms identity authentication

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

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.