Export (0) Print
Expand All
2 out of 29 rated this helpful - Rate this topic

FormsAuthentication Class

Provides static methods that supply helper utilities for manipulating authentication tickets. This class cannot be inherited.

For a list of all members of this type, see FormsAuthentication Members.

System.Object
   System.Web.Security.FormsAuthentication

[Visual Basic]
NotInheritable Public Class FormsAuthentication
[C#]
public sealed class FormsAuthentication
[C++]
public __gc __sealed class FormsAuthentication
[JScript]
public class FormsAuthentication

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Example

[Visual Basic] 
<!--
   The LoginBtn_Click event handler uses the Authenticate method to check the 
   user name and password entered by the user against the credentials stored 
   in the credential data store. If the username and password are correct as 
   verified by the authentication event handler function, the RedirectFromLoginPage 
   method transfers the user back to the protected resource. The 
   FormsAuthenticationEventHandler Delegate function is not included in this example.
-->
<%@ Page Language="VB" AutoEventWireup="true" %>
<html>
   <head>
      <script runat="server">
         Sub LoginBtn_Click(sender as Object, e as EventArgs) 
            If Page.IsValid Then
               ' Call the authentication event handler delegate (not included in this example).
               If FormsAuthentication.Authenticate(UserName.Text, UserPass.Text) Then
                  ' Return to the originally requested URL.
                  FormsAuthentication.RedirectFromLoginPage(UserName.Text, Remember.Checked)
               Else
                  Msg.Text = "Invalid Credentials: Please try again"
               End If
            End If    
         End Sub
      </script>

    </head>

   <body>
      <!-- 
         This form gets the user name and password from the user.
         The LoginBtn_Click event handler is called when the 
         Login button is clicked to authenticate the user.
      -->
      <form runat="server">
         <h2>Login Page</h2>
         <hr size="1" />
         <table>
            <tbody>
            <tr>
               <td>Username:</td>
               <td><asp:TextBox id="UserName" runat="server"></asp:TextBox></td>
               <td><asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" 
                  Display="Static" ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
               <td>Password:</td>
               <td><asp:TextBox id="UserPass" runat="server" TextMode="Password"></asp:TextBox></td>
               <td><asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="UserPass" 
                  Display="Static" ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
               <td colspan="3">&nbsp;<asp:CheckBox id="Remember" runat="server"></asp:CheckBox>Remember credentials?</td>
            </tr>
         </tbody>
         </table>
         <asp:button id="LoginBtn" onclick="LoginBtn_Click" runat="server" text="Login"></asp:button>
         <p><asp:Label id="Msg" runat="server" ForeColor="red"></asp:Label></p>
      </form>
   </body>
</html>

[C#] 
<!--
   The LoginBtn_Click event handler uses the Authenticate method to check the 
   user name and password entered by the user against the credentials stored 
   in the credential data store. If the username and password are correct as 
   verified by the authentication event handler function, the RedirectFromLoginPage 
   method transfers the user back to the protected resource. The 
   FormsAuthenticationEventHandler Delegate function is not included in this example.
-->
<%@ Page Language="C#" AutoEventWireup="true" %>
<html>
   <head>
      <script runat="server">
         void LoginBtn_Click(Object sender, EventArgs e) 
         {
            if (Page.IsValid) 
            {
               // Call the authentication event handler delegate (not included in this example).
               if (FormsAuthentication.Authenticate(UserName.Text, UserPass.Text)) 
               {
                  // Return to the originally requested URL.
                  FormsAuthentication.RedirectFromLoginPage(UserName.Text, Remember.Checked);
               }
               else 
               {
                  Msg.Text = "Invalid Credentials: Please try again";
               }
            }
         }
      </script>
   </head>

   <body>
      <!-- 
         This form gets the user name and password from the user.
         The LoginBtn_Click event handler is called when the 
         Login button is clicked to authenticate the user.
      -->
      <form runat="server">
         <h2>Login Page</h2>
         <hr size="1" />
         <table>
            <tbody>
            <tr>
               <td>Username:</td>
               <td><asp:TextBox id="UserName" runat="server"></asp:TextBox></td>
               <td><asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="UserName" 
                  Display="Static" ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
               <td>Password:</td>
               <td><asp:TextBox id="UserPass" runat="server" TextMode="Password"></asp:TextBox></td>
               <td><asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="UserPass" 
                  Display="Static" ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
               <td colspan="3">&nbsp;<asp:CheckBox id="Remember" runat="server"></asp:CheckBox>Remember credentials?</td>
            </tr>
         </tbody>
         </table>
         <asp:button id="LoginBtn" onclick="LoginBtn_Click" runat="server" text="Login"></asp:button>
         <p><asp:Label id="Msg" runat="server" ForeColor="red"></asp:Label></p>
      </form>
   </body>
</html>

[Visual Basic] 
[C#] 

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.Security

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

FormsAuthentication Members | System.Web.Security Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.