0

我正在使用 C# 和 .Net 当身份验证(使用成员资格提供程序)失败时,我需要将用户重定向到特定的 URL。

我在想用

RedirectToLoginPage(String)

从 MSDN:使用指定的查询字符串将浏览器重定向到登录 URL

但我需要更改网址。

使用示例:

 if (!Membership.ValidateUser(userName, password))
    {// do smt here}  

任何其他想法如何解决它?

4

3 回答 3

2

对我来说,这真的有效

aspx:

Username: <br />
<asp:TextBox runat="server" ID="txtUserName"></asp:TextBox>
<br />
Password: <br />
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox>
<br />
<asp:Button runat="server" ID="btnLogin" Text="Login" onclick="btnLogin_Click" 
    style="height: 26px" />

后面的代码:

protected void btnLogin_Click(object sender, EventArgs e)
{
   string username = txtUserName.Text.Trim();
   string password = txtPassword.Text.Trim();

   if (Membership.ValidateUser(username, password))
   {
     //...             
   }
   else
   {
       Response.Redirect("Hello.aspx");
   }
}
于 2013-02-12T07:43:05.323 回答
1

在发布用于检查用户有效性的凭据时,还要传递“return-to-url”查询参数或其他内容。在背后的逻辑中,以编程方式检查用户的有效性,如果用户成功通过身份验证,则重定向到给定的 URL,或者在您的情况下,如果身份验证失败,则重定向到某个页面。

您可以做的另一件事是,如果身份验证失败,您想要重定向,那么您可以处理由于身份验证客户端失败而导致的 401 http 状态,并从那里重定向到您想要的页面。

于 2013-02-12T07:34:42.273 回答
0

根据这个https://stackoverflow.com/a/749257/1495554没有办法使用标准方法来做你想做的事,@ZedBee 写了正确的解决方案

于 2013-02-12T07:52:03.963 回答