2011-08-10 34 views
3

我在Asp.Net中有一個應用程序,點擊一個按鈕它應該啓動另一個映射應用程序。在該應用程序中,用戶名和Email等用戶的憑據是必需的。所以,我試圖設置一個cookie並將該cookie的域名修復到該應用程序,但我無法在該應用程序中看到該cookie。我不確定發生了什麼問題,或者如果我在Cookie中犯了一些錯誤。在Asp.Net的另一個應用程序中讀取一個cookie

  MembershipUser usr = Membership.GetUser(); 
    Guid newUserId = (Guid)usr.ProviderUserKey; 
    HttpCookie SampleCookie = new HttpCookie("UserInfo"); 
    Response.Cookies["UserInfo"]["UserName"] = usr.UserName; 
    Response.Cookies["UserInfo"]["Email"] = usr.Email; 
    SampleCookie.Expires = DateTime.Now.AddDays(1); 
    Response.Cookies.Add(SampleCookie); 
    SampleCookie.Domain = "http://157.182.212.204/MAP"; 

再次感謝您的幫助。

代碼地圖應用:

  function Get_Cookie(check_name) { 
      // first we'll split this cookie up into name/value pairs 
      // note: document.cookie only returns name=value, not the other components 

      var a_all_cookies = document.cookie.split(';'); 
      var a_temp_cookie = ''; 
      var cookie_name = ''; 
      var cookie_value = ''; 
      var b_cookie_found = false; // set boolean t/f default f 

      for (i = 0; i < a_all_cookies.length; i++) 
      { 
       // now we'll split apart each name=value pair 
       a_temp_cookie = a_all_cookies[i].split('='); 


       // and trim left/right whitespace while we're at it 
       cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, ''); 

       // if the extracted name matches passed check_name 
       if (cookie_name == check_name) 
       { 
        b_cookie_found = true; 
        // we need to handle case where cookie has no value but exists (no = sign, that is): 
        if (a_temp_cookie.length > 1) 
        { 
         cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, '')); 
        } 
        // note that in cases where cookie is initialized but no value, null is returned 
        return cookie_value; 
        break; 
       } 
       a_temp_cookie = null; 
       cookie_name = ''; 
      } 
      if (!b_cookie_found) 
      { 
       return null; 
      } 
     } 

     function Delete_Cookie(name, path, domain) { 
      if (Get_Cookie(name)) document.cookie = name + "=" + 
        ((path) ? ";path=" + path : "") + 
        ((domain) ? ";domain=" + domain : "") + 
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT"; 
     } 

        alert(Get_Cookie("UserName"));               

爲WVWRAPICt頁RESET.aspx.cs下面給出的代碼...這是該cookie被設置

using System; 
    using System.Collections; 
    using System.Configuration; 
    using System.Collections.Generic; 
    using System.Data; 
    using System.Data.SqlClient; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Xml.Linq; 
    public partial class RESET_RESET : System.Web.UI.Page 
    { 
protected void Page_Load(object sender, EventArgs e) 
{ 
      Menu Nav = Master.FindControl("NavigationMenu1") as Menu; 
    MenuItemCollection Menu = Nav.Items; 
    foreach (MenuItem item in Menu) 
    { 
     string name = item.Text.ToString(); 
     if (name == "ADMIN") 
     { 
      item.Enabled = User.IsInRole("Administrator"); 
     } 
     if (name == "ICT") 
     { 
      item.Selected = true; 
     } 
     else 
     { 
      item.Selected = false; 
     } 
    } 
} 

protected void Button2_Click(object sender, EventArgs e) 
{ 
    MembershipUser usr = Membership.GetUser(); 
    Guid newUserId = (Guid)usr.ProviderUserKey; 
    HttpCookie SampleCookie = new HttpCookie("UserInfo"); 
    SampleCookie["UserName"] = usr.UserName; 
    SampleCookie["Email"] = usr.Email; 
    string connectionString = 
    ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 

    string checkSiteEventIDSQL = "Select * from UserProfiles WHERE UserId='" + newUserId + "'"; 

    using (SqlConnection myConnection1 = new SqlConnection(connectionString)) 
    { 
     try 
     { 
      myConnection1.Open(); 
      SqlCommand myCommand1 = new SqlCommand(checkSiteEventIDSQL, myConnection1); 
      SqlDataReader myReader = myCommand1.ExecuteReader(); 
      if (myReader.HasRows) 
      { 
       while (myReader.Read()) 
       { 
        string Agency = (myReader.GetValue(2)).ToString(); 
        SampleCookie["Agency"] = Agency; 

       } 


      } 
     } 
     catch (Exception ex) 
     { 

     } 
     finally 
     { 
      myConnection1.Close(); 
     } 
    } 
    SampleCookie.Expires = DateTime.Now.AddDays(1); 
    SampleCookie.Domain = "157.182.212.204/MAP"; 
    // SampleCookie.Path = "/MAP"; 
    Response.Cookies.Add(SampleCookie); 

    Response.Redirect("http://157.182.212.204/MAP/index.html"); 
} 
} 

回答

1

至於麻煩用你設置你的cookie的方式......除非你已經將它添加到響應中,否則你不會在響應中找到cookie。 (如果你找到它,你只需在幾行之後重寫這個cookie)。只需直接編輯cookie,然後添加到cookie jar。我也相信MAP應該在cookie的路徑屬性中(不知道它有多大的差異)。據我所知,你不需要在域中的http(再次,不知道瀏覽器是否足夠聰明來處理)。

MembershipUser usr = Membership.GetUser(); 
    Guid newUserId = (Guid)usr.ProviderUserKey; 
    HttpCookie sampleCookie = new HttpCookie("UserInfo"); 
    sampleCookie["UserName"] = usr.UserName; 
    sampleCookie["Email"] = usr.Email; 
    sampleCookie.Expires = DateTime.Now.AddDays(1); 
    sampleCookie.Domain = "157.182.212.204"; 
    sampleCookie.Path = "/MAP"; 
    Response.Cookies.Add(sampleCookie); 

Cookie只能設置爲當前FQDN的「尾部」的域。因此,如果您當前的FQDN不是157.182.212.204,則cookie不會在瀏覽器中設置。例如,尾巴,我的意思是http://overflow.acme.com可以爲overflow.acme.com或acme.com設置cookie,但不能爲fubar.acme.com或fubar.com設置Cookie。

我的猜測是,如果您的應用程序與MAP應用程序位於不同的FQDN上,則需要採用不同的方式將用戶名和電子郵件傳遞給地圖應用程序(可能發佈到頁面在地圖應用程序,可以設置Cookie,然後重定向到相應的頁面


更新您發佈更多的代碼後:

試試這個:

SampleCookie.Domain = "157.182.212.204"; 
    SampleCookie.Path = "/MAP"; 
    Response.Cookies.Add(SampleCookie); 

    Response.Redirect("http://157.182.212.204/MAP/index.html", false); 

在response.redirect上設置false應該會導致設置的cookie標頭通過。如果渲染事件中有任何東西,您可能需要短路頁面中的其他邏輯

或者只是將查詢字符串中的內容傳遞給您。你不使用HttpOnly cookies(所以用戶可以注入cookie)。

+0

我試過,但它似乎沒有工作...任何其他想法plzzzzz – user613037

+0

更新了更多的信息。 –

+0

這也沒有幫助。實際上我的應用程序是在157.182.212.204/WVWRAPICT,並點擊一個按鈕它調用157.182.212.204/MAP,所以我猜這是相同的FQDN。我不知道還有什麼可能是問題...請幫助,這是我第一次做這類事情。我真的很感謝你的幫助.... – user613037

相關問題