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

FormsAuthentication.GetRedirectUrl Method

Returns the redirect URL for the original request that caused the redirect to the logon page.

[Visual Basic]
Public Shared Function GetRedirectUrl( _
   ByVal userName As String, _
   ByVal createPersistentCookie As Boolean _
) As String
[C#]
public static string GetRedirectUrl(
 string userName,
 bool createPersistentCookie
);
[C++]
public: static String* GetRedirectUrl(
 String* userName,
 bool createPersistentCookie
);
[JScript]
public static function GetRedirectUrl(
   userName : String,
 createPersistentCookie : Boolean
) : String;

Parameters

userName
Name of the authenticated user. This does not have to map to a Windows account.
createPersistentCookie
Specifies whether or not a durable cookie (a cookie that is saved across browser sessions) should be issued. Cookie path defaults to'/'.

Return Value

A string containing the redirect URL.

Remarks

If there is no original URL, Default.aspx is used. If the redirect URL specifies a different server, this method also returns the authentication ticket as part of the query string. This method can be used by applications that need to do the redirect themselves instead of using RedirectFromLoginPage.

Example

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="true" %>
<script runat="server">

    Function Authenticated(email As String, password As String) As Boolean
    ' This method authenticates the user for the application.
    ' In this demonstration application it always returns
    ' true.

    Return True
    End Function

    Sub Login_Click(sender As Object, e As EventArgs)
    ' Create a custom FormsAuthenticationTicket containing
    ' application specific data for the user.

    Dim email As String         = UserEmail.Text
    Dim password As String      = UserPass.Text
    Dim isPersistent As Boolean = Persist.Checked

    if Authenticated(email,password) Then 

         Dim userData As String = "ApplicationSpecific data for this user."

       Dim ticket As New FormsAuthenticationTicket( _
        1, _
        email, _
        System.DateTime.Now, _
        System.DateTime.Now.AddMinutes(30), _
        isPersistent, _
        userData, _
        FormsAuthentication.FormsCookiePath)

       ' Encrypt the ticket.
       Dim encTicket As String = FormsAuthentication.Encrypt(ticket)

       ' Create the cookie.
       Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket))

       ' Redirect back to original URL.
       Response.Redirect(FormsAuthentication.GetRedirectUrl(email,isPersistent))
    End If
    End Sub

</script>
<html>
<head>
    <title>Forms Authentication Login</title>
</head>
<body>
    <form runat="server">
        <span style="BACKGROUND: #80ff80"> 
        <h3>Login Page</font>
        </h3>
        </span> 
        <table>
            <tbody>
                <tr>
                    <td>
                        e-mail:</td>
                    <td>
                        <asp:TextBox id="UserEmail" type="text" runat="server" /></td>
                    <td>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*" Display="Static" ControlToValidate="UserEmail"></ASP:RequiredFieldValidator>
                    </td>
                    <td>
                        <asp:RegularExpressionValidator id="RegexValidator" runat="server" ErrorMessage="Invalid format for e-mail address." Display="Static" ControlToValidate="UserEmail" EnableClientScript="false" ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Password:</td>
                    <td>
                        <asp:TextBox id="UserPass" TextMode="Password" runat="server" /></td>
                    <td>
                        <ASP:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="*" Display="Static" ControlToValidate="UserPass"></ASP:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Persistent Cookies:</td>
                    <td>
                        <asp:CheckBox id="Persist" runat="server" autopostback="true"></ASP:CheckBox>
                    </td>
                    <td>
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" value="Login" runat="server" onserverclick="Login_Click" />
        <p>
            <asp:Label id="Msg" runat="server" ></asp:Label>
        </p>
    </form>
</body>
</html>

[C#] 
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">

    private bool Authenticated(string email, string password)
    {
    // This method authenticates the user for the application.
    // In this demonstration application it always returns
    // true.

    return true;
    }

    private void Login_Click(Object sender, EventArgs e)
    {
    // Create a custom FormsAuthenticationTicket containing
    // application specific data for the user.

    string email        = UserEmail.Text;
    string password     = UserPass.Text;
    bool   isPersistent = Persist.Checked;

    if (Authenticated(email,password)) {

         string userData = "ApplicationSpecific data for this user.";

       FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1,
        email,
        System.DateTime.Now,
        System.DateTime.Now.AddMinutes(30),
        isPersistent,
        userData,
        FormsAuthentication.FormsCookiePath);

       // Encrypt the ticket.
       string encTicket = FormsAuthentication.Encrypt(ticket);

       // Create the cookie.
       Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

       // Redirect back to original URL.
       Response.Redirect(FormsAuthentication.GetRedirectUrl(email,isPersistent));
    }
    }

</script>
<html>
<head>
    <title>Forms Authentication Login</title>
</head>
<body>
    <form runat="server">
        <span style="BACKGROUND: #80ff80"> 
        <h3>Login Page</font>
        </h3>
        </span> 
        <table>
            <tbody>
                <tr>
                    <td>
                        e-mail:</td>
                    <td>
                        <asp:TextBox id="UserEmail" type="text" runat="server" /></td>
                    <td>
                        <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*" Display="Static" ControlToValidate="UserEmail"></ASP:RequiredFieldValidator>
                    </td>
                    <td>
                        <asp:RegularExpressionValidator id="RegexValidator" runat="server" ErrorMessage="Invalid format for e-mail address." Display="Static" ControlToValidate="UserEmail" EnableClientScript="false" ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Password:</td>
                    <td>
                        <asp:TextBox id="UserPass" TextMode="Password" runat="server" /></td>
                    <td>
                        <ASP:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="*" Display="Static" ControlToValidate="UserPass"></ASP:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Persistent Cookies:</td>
                    <td>
                        <asp:CheckBox id="Persist" runat="server" autopostback="true"></ASP:CheckBox>
                    </td>
                    <td>
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" value="Login" runat="server" onserverclick="Login_Click" />
    </form>
</body>
</html>

[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

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

See Also

FormsAuthentication Class | 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.