FormsAuthentication.RedirectToLoginPage 方法

定义

将浏览器重定向到登录 URL。

重载

RedirectToLoginPage()

将浏览器重定向到登录 URL。

RedirectToLoginPage(String)

将浏览器重定向到带有指定查询字符串的登录 URL。

示例

下面的代码示例使用 SignOut 方法清除 forms-authentication Cookie,并使用 方法将用户重定向到登录页 RedirectToLoginPage

<%@ 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 LoginLink_OnClick(object sender, EventArgs args)
{
  FormsAuthentication.SignOut();
  FormsAuthentication.RedirectToLoginPage();
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

<form id="form1" runat="server">
Welcome <b><%=User.Identity.Name%></b>. Not <b><%=User.Identity.Name%></b>? 
Click <asp:LinkButton id="LoginLink" Text="here" 
                      OnClick="LoginLink_OnClick" runat="server" />
to sign in.

<!-- Page Contents -->

</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 LoginLink_OnClick(sender As Object, args As EventArgs)
  FormsAuthentication.SignOut()
  FormsAuthentication.RedirectToLoginPage()
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

<form id="form1" runat="server">
Welcome <b><%=User.Identity.Name%></b>. Not <b><%=User.Identity.Name%></b>? 
Click <asp:LinkButton id="LoginLink" Text="here" 
                      OnClick="LoginLink_OnClick" runat="server" />
to sign in.

<!-- Page Contents -->

</form>



</body>
</html>

注解

方法 RedirectToLoginPage 将浏览器重定向到 LoginUrl

方法 RedirectToLoginPage 不清除 forms-authentication Cookie。 可以将 RedirectToLoginPage 方法与 SignOut 方法结合使用,以注销一个用户并允许其他用户登录。

HttpResponse.Redirect与 方法不同,此方法不会通过调用 HttpResponse.End结束请求。 这意味着将运行方法调用后面的 RedirectToLoginPage 代码。

RedirectToLoginPage()

将浏览器重定向到登录 URL。

public:
 static void RedirectToLoginPage();
public static void RedirectToLoginPage ();
static member RedirectToLoginPage : unit -> unit
Public Shared Sub RedirectToLoginPage ()

示例

下面的代码示例使用 SignOut 方法清除 forms-authentication Cookie,并使用 方法将用户重定向到登录页 RedirectToLoginPage

<%@ 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 LoginLink_OnClick(object sender, EventArgs args)
{
  FormsAuthentication.SignOut();
  FormsAuthentication.RedirectToLoginPage();
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

<form id="form1" runat="server">
Welcome <b><%=User.Identity.Name%></b>. Not <b><%=User.Identity.Name%></b>? 
Click <asp:LinkButton id="LoginLink" Text="here" 
                      OnClick="LoginLink_OnClick" runat="server" />
to sign in.

<!-- Page Contents -->

</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 LoginLink_OnClick(sender As Object, args As EventArgs)
  FormsAuthentication.SignOut()
  FormsAuthentication.RedirectToLoginPage()
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>

<form id="form1" runat="server">
Welcome <b><%=User.Identity.Name%></b>. Not <b><%=User.Identity.Name%></b>? 
Click <asp:LinkButton id="LoginLink" Text="here" 
                      OnClick="LoginLink_OnClick" runat="server" />
to sign in.

<!-- Page Contents -->

</form>



</body>
</html>

注解

方法 RedirectToLoginPage 将浏览器重定向到 LoginUrl

方法 RedirectToLoginPage 不清除 forms-authentication Cookie。 可以将 RedirectToLoginPage 方法与 SignOut 方法结合使用,以注销一个用户并允许其他用户登录。

另请参阅

适用于

RedirectToLoginPage(String)

将浏览器重定向到带有指定查询字符串的登录 URL。

public:
 static void RedirectToLoginPage(System::String ^ extraQueryString);
public static void RedirectToLoginPage (string extraQueryString);
static member RedirectToLoginPage : string -> unit
Public Shared Sub RedirectToLoginPage (extraQueryString As String)

参数

extraQueryString
String

要包含在重定向 URL 中的查询字符串。

示例

下面的代码示例使用 SignOut 方法清除 forms-authentication Cookie,并使用 方法将用户重定向到登录页 RedirectToLoginPage

<%@ 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 LoginLink_OnClick(object sender, EventArgs args)
{
  FormsAuthentication.SignOut();
  FormsAuthentication.RedirectToLoginPage(GetQueryString());
}

private string GetQueryString()
{
  string queryString = "";

  NameValueCollection qs = Request.QueryString;

  foreach (string key in qs.AllKeys)
    foreach (string value in qs.GetValues(key))
      queryString += Server.UrlEncode(key) + "=" + Server.UrlEncode(value) + "&";

  return queryString.TrimEnd('&');    
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
Welcome <b><%=User.Identity.Name%></b>. Not <b><%=User.Identity.Name%></b>? 
Click <asp:LinkButton id="LoginLink" Text="here" 
                      OnClick="LoginLink_OnClick" runat="server" />
to sign in.

<!-- Page Contents -->

</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 LoginLink_OnClick(sender As Object, args As EventArgs)
  FormsAuthentication.SignOut()
  FormsAuthentication.RedirectToLoginPage(GetQueryString())
End Sub

Private Function GetQueryString() As String
  Dim queryString As String = ""

  Dim qs As NameValueCollection = Request.QueryString

  For Each key As String In qs.AllKeys
    For Each value As String In qs.GetValues(key)
      queryString &= Server.UrlEncode(key) & "=" & Server.UrlEncode(value) & "&"
    Next
  Next

  Return queryString.TrimEnd("&")  
End Function

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
Welcome <b><%=User.Identity.Name%></b>. Not <b><%=User.Identity.Name%></b>? 
Click <asp:LinkButton id="LoginLink" Text="here" 
                      OnClick="LoginLink_OnClick" runat="server" />
to sign in.

<!-- Page Contents -->

</form>



</body>
</html>

注解

方法RedirectToLoginPage将浏览器重定向到 ,LoginUrl并将 值作为QueryString重定向 URL 的 包含extraQueryString

方法 RedirectToLoginPage 不清除 forms-authentication Cookie。 可以将 RedirectToLoginPage 方法与 SignOut 方法结合使用,以注销一个用户并允许其他用户登录。

另请参阅

适用于