【问题标题】:response redirect vs http request get response响应重定向与 http 请求获取响应
【发布时间】:2014-09-18 22:38:18
【问题描述】:

我想要做的是将 ASP 应用程序与 MVC 应用程序链接起来。现在 MVC 应用程序包含在我的 ASP 应用程序的框架中。我想要做的是,当我单击 ASP 应用程序的注销按钮时,也会调用 MVC 应用程序的注销方法。

现在我的登出页面是这样的:

    <% Dim xmlhttp, cookie
      'xmlHttp makes and async call to a POST method which finishes the ASP application session    
       Session.Abandon     
      'I execute methods to set the cookie value to an empty string
    %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Logout</title>
</head>
<body onload="document.forms[0].submit();">
<form method="POST" action="/end_MVC_Application.aspx">  
  <input type="submit" style="display:none" />
</form>
</body>
</html>

end_MVC_Application 页面实际上是空的:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="end_MVC_Application.aspx.cs" Inherits="end_MVC_Application" %>

但是后面的代码有以下逻辑。我在这里所做的是通过重定向页面调用注销 MVC 应用程序的方法来调用注销 MVC 特定方法。请注意,Logout 方法不会返回 MVC 上的视图,因为我不需要。

protected void Page_Load(object sender, EventArgs e)
{

    string url = "http:localhost/MyController/Logout";
    Response.Redirect(url,true);            
}

这工作正常,MVC 应用程序实际上正在注销。但是我需要重定向到 ASP 应用程序的实际登录页面。我尝试了不同的方法,但似乎都没有奏效。我已经尝试将之前方法的 Response.Redirect 改成这样:

 protected void Page_Load(object sender, EventArgs e)
    {
        string url = "http:localhost/MyController/Logout";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Response.Write("<script>");
        Response.Write("window.open('http://localhost/login.asp" + "', '_top')");
        Response.Write("</script>");
    }

通过这种方式,我已经验证了 MVC 应用程序上的注销方法正在被调用,并且应用程序正在被重定向到登录页面。但是,即使正在调用注销方法,这也不会关闭 MVC 应用程序。 我不明白为什么 Response.Redirect 行为不同。我猜它与结束响应的“真实”布尔值有关,但我不明白。

如果您能帮我解决这个问题,我将不胜感激。问候,路易斯。

【问题讨论】:

    标签: c# asp.net asp.net-mvc redirect


    【解决方案1】:

    登录票是存储在客户端浏览器上的 cookie。重定向到注销有效,因为它告诉客户端浏览器使用 http 302 响应重定向,然后您的注销代码使 cookie 过期。

    在您的第二次尝试中,您在服务器端使用 WebRequest 发出请求,客户端对此一无所知,并且仍然存储了有效的登录 cookie。

    也许您的注销控制器可以采用 ReturnUrl 参数并使用该 url 进行重定向?

    /MyController/Logout?ReturnUrl=/login.aspx
    

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多