FormsAuthentication.RedirectFromLoginPage 方法

定义

将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

重载

RedirectFromLoginPage(String, Boolean)

将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

RedirectFromLoginPage(String, Boolean, String)

使用 Forms 身份验证 Cookie 的指定 Cookie 路径,将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

RedirectFromLoginPage(String, Boolean)

将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

public:
 static void RedirectFromLoginPage(System::String ^ userName, bool createPersistentCookie);
public static void RedirectFromLoginPage (string userName, bool createPersistentCookie);
static member RedirectFromLoginPage : string * bool -> unit
Public Shared Sub RedirectFromLoginPage (userName As String, createPersistentCookie As Boolean)

参数

userName
String

经过身份验证的用户名。

createPersistentCookie
Boolean

若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false

例外

查询字符串中指定的返回 URL 包含 HTTP: 或 HTTPS: 以外的协议。

示例

下面的代码示例将已验证的用户重定向到最初请求的 URL 或 DefaultUrlURL。 该代码示例使用 ASP.NET 成员身份来验证用户。 有关 ASP.NET 成员身份的详细信息,请参阅使用成员身份管理用户

重要

此示例包含接受用户输入的文本框,这是潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Login_OnClick(object sender, EventArgs args)
{
   if (Membership.ValidateUser(UsernameTextbox.Text, PasswordTextbox.Text))
      FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text, NotPublicCheckBox.Checked);
   else
     Msg.Text = "Login failed. Please check your user name and password and try again.";
}


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Login</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Login</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  Username: <asp:Textbox id="UsernameTextbox" runat="server" /><br />
  Password: <asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /><br />
 
  <asp:Button id="LoginButton" Text="Login" OnClick="Login_OnClick" runat="server" />
  <asp:CheckBox id="NotPublicCheckBox" runat="server" /> 
  Check here if this is <span style="text-decoration:underline">not</span> a public computer.

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Login_OnClick(sender As Object, args As  EventArgs)

   If (Membership.ValidateUser(UsernameTextbox.Text, PasswordTextbox.Text)) Then
      FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text, NotPublicCheckBox.Checked)
   Else
     Msg.Text = "Login failed. Please check your user name and password and try again."
   End If

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>Login</title>
</head>
<body>

<form id="form1" runat="server">
  <h3>Login</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  Username: <asp:Textbox id="UsernameTextbox" runat="server" /><br />
  Password: <asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /><br />
 
  <asp:Button id="LoginButton" Text="Login" OnClick="Login_OnClick" runat="server" />
  <asp:CheckBox id="NotPublicCheckBox" runat="server" /> 
  Check here if this is <span style="text-decoration:underline">not</span> a public computer.

</form>

</body>
</html>

注解

该方法 RedirectFromLoginPage 使用 ReturnURL 变量名称重定向到查询字符串中指定的 URL。 例如,在 URL http://www.contoso.com/login.aspx?ReturnUrl=caller.aspx中,该方法 RedirectFromLoginPage 重定向到返回 URL caller.aspxReturnURL如果变量不存在,该方法RedirectFromLoginPage将重定向到属性中的 DefaultUrl URL。

ASP.NET 将浏览器重定向到登录页时自动添加返回 URL。

默认情况下,该 ReturnUrl 变量必须引用当前应用程序中的页面。 如果 ReturnUrl 引用不同应用程序或不同服务器上的页面,则 RedirectFromLoginPage 方法会重定向到属性中的 DefaultUrl URL。 如果要允许重定向到当前应用程序外部的页面,则必须使用窗体配置元素的属性将属性true设置为EnableCrossAppRedirects使用enableCrossAppRedirects

重要

EnableCrossAppRedirects 属性设置为 true 允许跨应用程序重定向是潜在的安全威胁。 有关更多信息,请参见 EnableCrossAppRedirects 属性。

CookiesSupported如果属性是true,并且ReturnUrl变量位于当前应用程序或EnableCrossAppRedirects属性true中,则RedirectFromLoginPage该方法会发出身份验证票证,并使用该方法将其置于默认 Cookie SetAuthCookie 中。

false如果是CookiesSupported并且重定向路径是当前应用程序中的 URL,则票证作为重定向 URL 的一部分颁发。 如果是CookiesSupportedEnableCrossAppRedirectstrue重定向 URL 不引用当前应用程序中的页面,该方法RedirectFromLoginPage会发出身份验证票证并将其置于属性中QueryStringfalse

另请参阅

适用于

RedirectFromLoginPage(String, Boolean, String)

使用 Forms 身份验证 Cookie 的指定 Cookie 路径,将经过身份验证的用户重定向回最初请求的 URL 或默认 URL。

public:
 static void RedirectFromLoginPage(System::String ^ userName, bool createPersistentCookie, System::String ^ strCookiePath);
public static void RedirectFromLoginPage (string userName, bool createPersistentCookie, string strCookiePath);
static member RedirectFromLoginPage : string * bool * string -> unit
Public Shared Sub RedirectFromLoginPage (userName As String, createPersistentCookie As Boolean, strCookiePath As String)

参数

userName
String

经过身份验证的用户名。

createPersistentCookie
Boolean

若要创建持久 Cookie(跨浏览器会话保存的 Cookie),则为 true;否则为 false

strCookiePath
String

Forms 身份验证票证的 Cookie 路径。

例外

查询字符串中指定的返回 URL 包含 HTTP: 或 HTTPS: 以外的协议。

注解

该方法 RedirectFromLoginPage 使用 ReturnURL 变量名称重定向到查询字符串中指定的返回 URL。 例如,在 URL http://www.contoso.com/login.aspx?ReturnUrl=caller.aspx中,该方法 RedirectFromLoginPage 重定向到返回 URL caller.aspxReturnURL如果变量不存在,该方法RedirectFromLoginPage将重定向到属性中的 DefaultUrl URL。

ASP.NET 将浏览器重定向到登录页时自动添加返回 URL。

默认情况下,该 ReturnUrl 变量必须引用当前应用程序中的页面。 如果 ReturnUrl 引用不同应用程序或不同服务器上的页面,该方法 RedirectFromLoginPage 将重定向到属性中的 DefaultUrl URL。 如果要允许重定向到当前应用程序外部的页面,则必须使用窗体配置元素的属性将属性true设置为EnableCrossAppRedirects使用enableCrossAppRedirects

重要

EnableCrossAppRedirects 属性设置为 true 允许跨应用程序重定向是潜在的安全威胁。 有关更多信息,请参见 EnableCrossAppRedirects 属性。

CookiesSupported如果属性是true,并且ReturnUrl变量位于当前应用程序或EnableCrossAppRedirects属性true中,则RedirectFromLoginPage该方法会发出身份验证票证,并使用该方法将它放在参数SetAuthCookie指定的 strCookiePath Cookie 中。

false如果是CookiesSupported并且重定向路径是当前应用程序中的 URL,则票证作为重定向 URL 的一部分颁发。 如果是CookiesSupportedEnableCrossAppRedirectstrue重定向 URL 不引用当前应用程序中的页面,该方法RedirectFromLoginPage会发出身份验证票证并将其置于属性中QueryStringfalse

另请参阅

适用于