FormsAuthentication.RedirectFromLoginPage FormsAuthentication.RedirectFromLoginPage FormsAuthentication.RedirectFromLoginPage FormsAuthentication.RedirectFromLoginPage Method

定義

將已驗證的使用者重新導向回到原來要求的 URL 或預設 URL。Redirects an authenticated user back to the originally requested URL or the default URL.

多載

RedirectFromLoginPage(String, Boolean) RedirectFromLoginPage(String, Boolean) RedirectFromLoginPage(String, Boolean) RedirectFromLoginPage(String, Boolean)

將已驗證的使用者重新導向回到原來要求的 URL 或預設 URL。Redirects an authenticated user back to the originally requested URL or the default URL.

RedirectFromLoginPage(String, Boolean, String) RedirectFromLoginPage(String, Boolean, String) RedirectFromLoginPage(String, Boolean, String) RedirectFromLoginPage(String, Boolean, String)

使用為表單驗證 Cookie 指定的 Cookie 路徑,將已驗證的使用者重新導向回到原先要求的 URL 或預設 URL。Redirects an authenticated user back to the originally requested URL or the default URL using the specified cookie path for the forms-authentication cookie.

RedirectFromLoginPage(String, Boolean) RedirectFromLoginPage(String, Boolean) RedirectFromLoginPage(String, Boolean) RedirectFromLoginPage(String, Boolean)

將已驗證的使用者重新導向回到原來要求的 URL 或預設 URL。Redirects an authenticated user back to the originally requested URL or the default 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 String String String

已驗證的使用者名稱。The authenticated user name.

createPersistentCookie
Boolean Boolean Boolean Boolean

true 表示建立持久性 Cookie (跨瀏覽器工作階段儲存的 Cookie),否則為 falsetrue to create a durable cookie (one that is saved across browser sessions); otherwise, false.

例外狀況

查詢字串中所指定的傳回 URL 含有 HTTP: 或 HTTPS: 以外的通訊協定。The return URL specified in the query string contains a protocol other than HTTP: or HTTPS:.

範例

下列程式碼範例將已驗證的使用者重新導向至其中一個原始要求的 URL 或DefaultUrlThe following code example redirects validated users to either the originally requested URL or the DefaultUrl. 在程式碼範例會使用 ASP.NET 成員資格,來驗證使用者。The code example uses ASP.NET membership to validate users. 如需有關 ASP.NET 成員資格的詳細資訊,請參閱使用的成員資格管理使用者For more information about ASP.NET membership, see Managing Users by Using Membership.

重要

此範例包含一個文字方塊,接受使用者輸入,也就是潛在的安全性威脅。This example contains a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<%@ 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方法會將重新導向至指定在查詢字串中使用的 URLReturnURL變數名稱。The RedirectFromLoginPage method redirects to the URL specified in the query string using the ReturnURL variable name. 例如,在 URL http://www.contoso.com/login.aspx?ReturnUrl=caller.aspx,則RedirectFromLoginPage方法會將重新導向至傳回 URL caller.aspxFor example, in the URL http://www.contoso.com/login.aspx?ReturnUrl=caller.aspx, the RedirectFromLoginPage method redirects to the return URL caller.aspx. 如果ReturnURL變數不存在,RedirectFromLoginPage方法會將重新導向至中的 URLDefaultUrl屬性。If the ReturnURL variable does not exist, the RedirectFromLoginPage method redirects to the URL in the DefaultUrl property.

瀏覽器重新導向至登入頁面時,ASP.NET 會自動將傳回的 URL。ASP.NET automatically adds the return URL when the browser is redirected to the login page.

根據預設,ReturnUrl變數必須參考目前的應用程式內的頁面。By default, the ReturnUrl variable must refer to a page within the current application. 如果ReturnUrl頁面在不同的應用程式中或在不同的伺服器,是指RedirectFromLoginPage方法會將重新導向至中的 URLDefaultUrl屬性。If ReturnUrl refers to a page in a different application or on a different server, the RedirectFromLoginPage methods redirects to the URL in the DefaultUrl property. 如果您想要允許重新導向至目前的應用程式外的網頁,您必須設定EnableCrossAppRedirects屬性,以true使用enableCrossAppRedirects屬性form組態項目。If you want to allow redirects to a page outside the current application, you must set the EnableCrossAppRedirects property to true using the enableCrossAppRedirects attribute of the forms configuration element.

重要

設定EnableCrossAppRedirects屬性設true以便跨應用程式重新導向是潛在的安全性威脅。Setting the EnableCrossAppRedirects property to true to allow cross-application redirects is a potential security threat. 如需詳細資訊,請參閱 EnableCrossAppRedirects 屬性 (Property)。For more information, see the EnableCrossAppRedirects property.

如果CookiesSupported屬性是true,以及ReturnUrl變數是在目前應用程式或EnableCrossAppRedirects屬性true,則RedirectFromLoginPage方法發出驗證票證,並將它放入預設會使用 cookieSetAuthCookie方法。If the CookiesSupported property is true, and either the ReturnUrl variable is within the current application or the EnableCrossAppRedirects property is true, then the RedirectFromLoginPage method issues an authentication ticket and places it in the default cookie using the SetAuthCookie method.

如果CookiesSupportedfalse而重新導向路徑中目前的應用程式的 url,票證會發出重新導向 URL 的一部分。If CookiesSupported is false and the redirect path is to a URL in the current application, the ticket is issued as part of the redirect URL. 如果CookiesSupportedfalseEnableCrossAppRedirectstrue,和在目前的應用程式頁面未參考的 URL 重新導向RedirectFromLoginPage方法發出驗證票證,並將它放入QueryString屬性。If CookiesSupported is false, EnableCrossAppRedirects is true, and the redirect URL does not refer to a page within the current application, the RedirectFromLoginPage method issues an authentication ticket and places it in the QueryString property.

另請參閱

RedirectFromLoginPage(String, Boolean, String) RedirectFromLoginPage(String, Boolean, String) RedirectFromLoginPage(String, Boolean, String) RedirectFromLoginPage(String, Boolean, String)

使用為表單驗證 Cookie 指定的 Cookie 路徑,將已驗證的使用者重新導向回到原先要求的 URL 或預設 URL。Redirects an authenticated user back to the originally requested URL or the default URL using the specified cookie path for the forms-authentication cookie.

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 String String String

已驗證的使用者名稱。The authenticated user name.

createPersistentCookie
Boolean Boolean Boolean Boolean

true 表示建立持久性 Cookie (跨瀏覽器工作階段儲存的 Cookie),否則為 falsetrue to create a durable cookie (one that is saved across browser sessions); otherwise, false.

strCookiePath
String String String String

表單驗證票證的 Cookie 路徑。The cookie path for the forms-authentication ticket.

例外狀況

查詢字串中所指定的傳回 URL 含有 HTTP: 或 HTTPS: 以外的通訊協定。The return URL specified in the query string contains a protocol other than HTTP: or HTTPS:.

備註

RedirectFromLoginPage方法會重新導向至在查詢字串中使用指定的傳回 URLReturnURL變數名稱。The RedirectFromLoginPage method redirects to the return URL specified in the query string using the ReturnURL variable name. 例如,在 URL http://www.contoso.com/login.aspx?ReturnUrl=caller.aspx,則RedirectFromLoginPage方法會將重新導向至傳回 URL caller.aspxFor example, in the URL http://www.contoso.com/login.aspx?ReturnUrl=caller.aspx, the RedirectFromLoginPage method redirects to the return URL caller.aspx. 如果ReturnURL變數不存在,RedirectFromLoginPage方法會將重新導向至中的 URLDefaultUrl屬性。If the ReturnURL variable does not exist, the RedirectFromLoginPage method redirects to the URL in the DefaultUrl property.

瀏覽器重新導向至登入頁面時,ASP.NET 會自動將傳回的 URL。ASP.NET automatically adds the return URL when the browser is redirected to the login page.

根據預設,ReturnUrl變數必須參考目前的應用程式內的頁面。By default, the ReturnUrl variable must refer to a page within the current application. 如果ReturnUrl頁面在不同的應用程式中或在不同的伺服器,是指RedirectFromLoginPage方法會將重新導向至中的 URLDefaultUrl屬性。If ReturnUrl refers to a page in a different application or on a different server, the RedirectFromLoginPage method redirects to the URL in the DefaultUrl property. 如果您想要允許重新導向至目前的應用程式外的網頁,您必須設定EnableCrossAppRedirects屬性,以true使用enableCrossAppRedirects屬性form組態項目。If you want to allow redirects to a page outside the current application, you must set the EnableCrossAppRedirects property to true using the enableCrossAppRedirects attribute of the forms configuration element.

重要

設定EnableCrossAppRedirects屬性設true以便跨應用程式重新導向是潛在的安全性威脅。Setting the EnableCrossAppRedirects property to true to allow cross-application redirects is a potential security threat. 如需詳細資訊,請參閱 EnableCrossAppRedirects 屬性 (Property)。For more information, see the EnableCrossAppRedirects property.

如果CookiesSupported屬性是true,以及ReturnUrl變數是在目前應用程式或EnableCrossAppRedirects屬性true,則RedirectFromLoginPage方法發出驗證票證,並將它放入所指定的 cookiestrCookiePath參數使用SetAuthCookie方法。If the CookiesSupported property is true, and either the ReturnUrl variable is within the current application or the EnableCrossAppRedirects property is true, then the RedirectFromLoginPage method issues an authentication ticket and places it in the cookie specified by the strCookiePath parameter using the SetAuthCookie method.

如果CookiesSupportedfalse而重新導向路徑中目前的應用程式的 url,票證會發出重新導向 URL 的一部分。If CookiesSupported is false and the redirect path is to a URL in the current application, the ticket is issued as part of the redirect URL. 如果CookiesSupportedfalseEnableCrossAppRedirectstrue,和在目前的應用程式頁面未參考的 URL 重新導向RedirectFromLoginPage方法發出驗證票證,並將它放入QueryString屬性。If CookiesSupported is false, EnableCrossAppRedirects is true, and the redirect URL does not refer to a page within the current application, the RedirectFromLoginPage method issues an authentication ticket and places it in the QueryString property.

另請參閱

適用於