簡體   English   中英

重定向 URL 不起作用

[英]Redirecting URL Not Working

我想在訪問所有網頁之前添加授權。 所以我在web.config中使用了以下配置

<authentication mode="Forms">
   <forms loginUrl="~/Login/Login.aspx" />
</authentication>
<authorization>
   <deny users="?"/>
</authorization>

在此之后對於每個頁面 Login.aspx 詢問,但在成功登錄后重定向不適用於以下代碼。

//http://localhost:55217/Login/Login.aspx?ReturnUrl=%2fHome%2fdeleteUser.aspx

if (returnMsg == "Success") {
    string query0 = Request.QueryString[0];
    finalStr = "~" + query0;
    Response.Redirect(finalStr, false);

    //Session["Login"] = username;
    //Response.Redirect("~/Home/Home.aspx");
    //Response.Redirect("/Home/HomeTest.aspx");
} else {
    StatusLabel.Attributes["style"] = "color:red; font-weight:bold;";
    StatusLabel.Text = "Error: Username or Password Wrong";
}

它再次停留在登錄頁面上,要求提供憑據。 但沒有顯示錯誤"Error: Username or Password Wrong"

任何想法為什么它不起作用?

如果您使用 Forms 身份驗證,則在身份驗證成功時您需要創建一個身份驗證 cookie。 否則 ASP.NET 子系統將不知道身份驗證成功。

請參閱本文: https : //support.microsoft.com/en-us/kb/301240

以下是這篇文章的相關文字:

4.您可以使用兩種方法之一來生成表單身份驗證cookie 並將用戶重定向到cmdLogin_ServerClick 事件中的適當頁面。 為這兩種情況都提供了示例代碼。 根據您的要求使用它們中的任何一個。

• 調用 RedirectFromLoginPage 方法自動生成表單身份驗證 cookie 並將用戶重定向到 cmdLogin_ServerClick 事件中的適當頁面:

private void cmdLogin_ServerClick(object sender, System.EventArgs e)
{
if (ValidateUser(txtUserName.Value,txtUserPass.Value) )
    FormsAuthentication.RedirectFromLoginPage(txtUserName.Value,
        chkPersistCookie.Checked);
    else
        Response.Redirect("logon.aspx", true);
}

• 生成身份驗證票,對其進行加密,創建一個 cookie,將其添加到響應中,並重定向用戶。 這使您可以更好地控制創建 cookie 的方式。 在這種情況下,您還可以包含自定義數據以及 FormsAuthenticationTicket。

private void cmdLogin_ServerClick(object sender, System.EventArgs e)
{
   if (ValidateUser(txtUserName.Value,txtUserPass.Value) )
   {
      FormsAuthenticationTicket tkt;
      string cookiestr;
      HttpCookie ck;
      tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now, 
DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");
      cookiestr = FormsAuthentication.Encrypt(tkt);
      ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
      if (chkPersistCookie.Checked)
      ck.Expires=tkt.Expiration;    
            ck.Path = FormsAuthentication.FormsCookiePath; 
      Response.Cookies.Add(ck);

      string strRedirect;
      strRedirect = Request["ReturnUrl"];
      if (strRedirect==null)
            strRedirect = "default.aspx";
         Response.Redirect(strRedirect, true);
   }
   else
      Response.Redirect("logon.aspx", true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM