2011-08-10 4 views
3

나는 Asp.Net에서 응용 프로그램을 가지고 있으며 버튼을 클릭하면 다른 매핑 응용 프로그램을 시작해야합니다. 해당 응용 프로그램에서 사용자 이름 및 전자 메일 같은 사용자의 자격 증명이 필요합니다. 그래서, 쿠키를 설정하고 그 응용 프로그램에 쿠키 도메인을 수정하려고했지만 해당 응용 프로그램에서 쿠키를 볼 수 없습니다. 나는 무엇이 잘못되고 있는지 또는 쿠키를 실수로 잘못 작성했는지 정말로 확신하지 못합니다.Asp.Net의 다른 응용 프로그램에서 쿠키 읽기

  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"; 

다시 한번 감사드립니다. 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의 코드 ... 이것은 쿠키

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

에 관한 문제 설정되는 쿠키를 설정하는 방법과 함께 ... 응답에 쿠키를 추가하지 않으면 응답에서 쿠키를 찾을 수 없습니다. (그리고 당신이 그것을 발견한다면, 당신은 단지 몇 줄 후에 그 쿠키를 덮어 쓰는 것입니다). 쿠키를 직접 편집 한 다음 쿠키 항아리에 추가하면됩니다. 또한 MAP는 쿠키의 경로 속성에 있어야한다고 생각합니다 (쿠키가 얼마나 큰 차이인지는 확실하지 않습니다). 내가 아는 한, 도메인에서 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); 

쿠키는 현재 FQDN의 '꼬리'인 도메인에만 설정할 수 있습니다. 따라서 현재 FQDN이 157.182.212.204가 아닌 경우 쿠키가 브라우저에 설정되지 않습니다. 예를 들어, 꼬리 부분은 http://overflow.acme.com이 overflow.acme.com 또는 acme.com에 대한 쿠키를 설정할 수 있지만 fubar.acme.com 또는 fubar.com에 대해서는 쿠키를 설정할 수 없다는 것을 의미합니다.

응용 프로그램이 MAP 응용 프로그램과 다른 FQDN을 사용하는 경우 맵 응용 프로그램에 사용자 이름과 전자 메일을 전달하는 다른 방법을 사용해야합니다 (아마 페이지에 게시 할 수 있습니다). ? 해당 페이지로 리디렉션 한 다음 쿠키를 설정할 수 있습니다지도 응용 프로그램에서 당신은 좀 더 코드를 게시 한 후


업데이트 :

이 시도 :

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를 설정하면 설정된 쿠키 헤더가 전달됩니다. 렌더링 이벤트에 아무 것도없는 경우 페이지의 다른 논리를 단락시켜야 할 수도 있습니다.

또는 쿼리 문자열에 항목을 전달하십시오. 당신은 HttpOnly 쿠키를 사용하지 않습니다 (그래서 사용자는 쿠키를 주입 할 수 있습니다).

+0

시도했지만 작동하지 않는 것 같습니다 ... 다른 아이디어 plzzzzz – user613037

+0

자세한 정보로 업데이트되었습니다. –

+0

그건 도움이되지 않았어. 사실 내 응용 프로그램은 157.182.212.204/WVWRAPICT이고 버튼을 클릭하면 157.182.212.204/MAP라고 부르기 때문에 동일한 FQDN이라고 생각합니다. 나는 그 밖의 무엇이 문제가 될지 확신하지 못합니다. 제발 도와주세요. 그리고 이것은 제가 이런 종류의 일을하고있는 주먹입니다. 도움을 주셔서 감사합니다 .... – user613037