2011-08-10 33 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

相关问题