您的位置:首页 > 其它

FormsAuthentication.GetRedirectUrl 方法

2015-12-09 11:08 453 查看
https://msdn.microsoft.com/zh-cn/library/8a22t5t3(v=vs.80)

FormsAuthentication.GetRedirectUrl 方法

.NET Framework 2.0

其他版本



返回导致重定向到登录页的原始请求的重定向 URL。

命名空间:System.Web.Security
程序集:System.Web(在 system.web.dll 中)

语法

C#
C++
VB

public static string GetRedirectUrl (
string userName,
bool createPersistentCookie
)


J#

public static String GetRedirectUrl (
String userName,
boolean createPersistentCookie
)


JScript

public static function GetRedirectUrl (
userName : String,
createPersistentCookie : boolean
) : String


参数

userName
已验证身份的用户的名称。

createPersistentCookie
忽略此参数。

返回值

一个字符串,其中包含重定向 URL。

备注

当您希望在应用程序代码中执行重定向时可使用此方法,而不使用 RedirectFromLoginPage 方法。

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

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

默认情况下,ReturnUrl 变量必须引用当前应用程序中的页。如果 ReturnUrl 引用其他应用程序或其他服务器中的页,GetRedirectUrl 方法将返回DefaultUrl 属性中的 URL。如果希望允许返回 URL 引用当前应用程序以外的页,必须使用 forms 配置元素的 enableCrossAppRedirects 属性 (Attribute) 将 EnableCrossAppRedirects 属性 (Property) 设置为 true


安全注意
EnableCrossAppRedirects 属性设置为 true 以允许跨应用程序重定向是一个潜在的安全威胁。如果允许跨应用程序重定向,您的站点将易于受到恶意网站的攻击,这些恶意网站将利用您的登录页使您的网站用户相信他们所使用的站点上的网页是安全的。若要提高使用跨应用程序重定向时的安全性,应该重写 GetRedirectUrl 方法,只允许重定向到许可的网站。

示例

下面的代码示例将通过身份验证的用户重定向到由 GetRedirectUrl 方法返回的 URL。

C#
VB

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

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

string username     = UserNameTextBox.Text;
string password     = UserPassTextBox.Text;
bool   isPersistent = PersistCheckBox.Checked;

if (Membership.ValidateUser(username, password))
{
string userData = "ApplicationSpecific data for this user.";

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
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(username, isPersistent));
}
else
{
Msg.Text = "Login failed. Please check your user name and password and try again.";
}
}

</script>
<html>
<head>
<title>Forms Authentication Login</title>
</head>
<body>
<form runat="server">
<span style="BACKGROUND: #80ff80">
<h3>Login Page</h3>
</span>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><P>
<table border=0>
<tbody>
<tr>
<td>Username:</td>
<td><asp:TextBox id="UserNameTextBox" type="text" runat="server" /></td>
<td>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="*"
Display="Static"
ControlToValidate="UserNameTextBox" />
</td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox id="UserPassTextBox" TextMode="Password" runat="server" /></td>
<td>
<asp:RequiredFieldValidator id="RequiredFieldValidator2"
runat="server" ErrorMessage="*"
Display="Static"
ControlToValidate="UserPassTextBox" />
</td>
</tr>
<tr>
<td>Check here if this is <u>not</u><br>a public computer:</td>
<td><asp:CheckBox id="PersistCheckBox" runat="server" autopostback="true" /></td>
</tr>
</tbody>
</table>
<input type="submit" value="Login" runat="server" onserverclick="Login_Click" />
</form>
</body>
</html>


平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: