2014-09-18 142 views
0

我想要做的是鏈接和ASP應用程序與MVC應用程序。現在,MVC應用程序包含在我的ASP應用程序框架中。我想要做的是當我點擊我的ASP應用程序的註銷按鈕時,MVC應用程序的註銷方法也被調用。響應重定向vs http請求獲取響應

現在我註銷頁面看起來像這樣:

<% 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的行爲不同。我猜測它與結束響應的「真正的」布爾值有關,但我不明白。

如果你能幫助我,我將不勝感激。問候,路易斯。

回答

0

登錄票據是存儲在客戶端瀏覽器中的cookie。重定向到註銷的工作原理是因爲它告訴客戶端瀏覽器重定向一個http 302響應,然後您的註銷代碼將過期cookie。

在第二次嘗試中,您在服務器端使用WebRequest發出請求時,客戶端對此一無所知,並且仍然存儲有效的登錄cookie。

也許您的註銷控制器可以採取ReturnUrl參數並使用該URL來重定向?

/MyController/Logout?ReturnUrl=/login.aspx