【问题标题】:Url skip second querystring parameter in HttpContext.Current.Request.Url.AbsoluteUri网址跳过 HttpContext.Current.Request.Url.AbsoluteUri 中的第二个查询字符串参数
【发布时间】:2013-06-22 01:27:46
【问题描述】:

我有一个类,我继承到所有页面来检查会话是否超时然后重定向到登录页面这里是代码

public class WMUserBase:System.Web.UI.Page
{
    public WMUserBase()
    {

    }
    protected override void OnLoad(System.EventArgs e)
    {
        CheckSecurity();
        base.OnLoad(e);
    }
    public virtual void CheckSecurity()
    {
        if (Session["WMuserId"] == null || Session["WMuserId"].ToString() == "")
        {
            Response.Redirect("Login.aspx?ReturnUrl=" + HttpContext.Current.Request.Url.AbsoluteUri);
        }
    }
}

每个页面都继承这个类,如果会话超时然后页面被重定向到登录页面现在我使用
Response.Redirect("Login.aspx?ReturnUrl=" + HttpContext.Current.Request.Url.AbsoluteUri); 但是如果我的查询字符串有两个参数,例如http://localhost/mala3ibnav2/WeeklyManager/TeamSetup.aspx?Gid=MTQ=&Mid=Mg== 在 AbsoluteUril &Mid=Mg== 中,有时并非总是会被跳过。
这是登录页面登录按钮点击事件的代码

 if(string.IsNullOrEmpty(ReturnUrl))
                {
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    Response.Redirect(ReturnUrl);
                }

现在为什么在查询字符串中跳过第二个参数,我应该使用什么来代替 HttpContext.Current.Request.Url.AbsoluteUri

【问题讨论】:

  • 您需要对 URL 进行编码,尤其是 & 符号。如果您检查 url,则不应在那里找到 &,但编码
  • 感谢帮助让我试试

标签: c# asp.net url absolute-path querystringparameter


【解决方案1】:

您必须对网址进行编码:

Response.Redirect("Login.aspx?ReturnUrl=" + HttpUtility.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri));

如果您使用表单身份验证,您可以将身份验证超时设置为比会话超时少一分钟。那么你就不必自己编写代码了,因为 ASP.NET 会在超时后自动将用户重定向到登录页面。

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多