请问各位,能否在不同站点之间使用cookie传递一些程序指定的信息呢?
就像各大网站的通行证概念;在使用通行证的任何一个站点登陆过以后,到达其它站点无需再次登陆?
请问如果这不是使用cookie实现的,究竟该如何实现呢?谢谢了

解决方案 »

  1.   

    原则上来说一个站点是不能读取另外站点的Cookie的。个人觉得那些passport都是模拟的。 即进入B站点以后 B发现没有登陆 随即将页面转向认证中心 认证中心判断一下这个人是否已经 登陆 如果登陆了就再次转向前一个页面 同时传递一定的密文(比如一个很长的字符串) 然后B根据这个字符串来解析出登陆者的信息 ,这个过程可能会访问公共数据库 。
      

  2.   

    可以用cookie来解决,但要域名一致,就是说,你设置所有的cookie的domain属性为同一个值
      

  3.   

    可以用数据库,或者web Services,也可以用cookie,参见http://www.tongyi.net/article/20030108/200301083561.shtml
      

  4.   

    public class SessionPage : System.Web.UI.Page 

     ... 
     public new mySession Session = null; 
     ... 
    }    自定义的对话类使用HybridDictionary对象来相应保存内存中的对话状态(HybridDictionary可用于处理任意数量的对话元素)。为了与传统的ASP通用,该自定义对话对象将限制对话数据类型为字符串型(默认的HttpSession允许对话保存任意类型的数据,不能与传统的ASP通用)。 
    [Serializable] 
    public class mySession  

     private HybridDictionary dic = new HybridDictionary();  public mySession() 
     { 
     }  public string this [string name] 
     { 
      get 
      { 
       return (string)dic[name.ToLower()]; 
      } 
      set 
      { 
       dic[name.ToLower()] = value; 
      } 
     } 
    }  
        Page类为定制暴露了不同的事件和方法。特别是OnInit方法用于设置Page对象的初始化状态。如果请求不包含名为mySession的Cookie,将为请求者产生一个新的mySession Cookie。另外,对话数据将使用自定义数据访问对象SessionPersistence从SQL Server中检索出来。DSN和SessionExpiration的值从web.config中检索。 
    override protected void OnInit(EventArgs e) 

     InitializeComponent(); 
     base.OnInit(e); 

    private void InitializeComponent() 
    {  
     cookie = this.Request.Cookies[sessionPersistence.SessionID];  if (cookie == null) 
     { 
      Session = new mySession(); 
      CreateNewSessionCookie(); 
      IsNewSession = true; 
     } 
     else 
      Session = sessionPersistence.LoadSession( 
        Server.UrlDecode(cookie.Value).ToLower().Trim(),  
        dsn,  
        SessionExpiration 
       );   this.Unload += new EventHandler(this.PersistSession); 

    private void CreateNewSessionCookie() 

     cookie = new HttpCookie(sessionPersistence.SessionID,  
     sessionPersistence.GenerateKey()); 
     this.Response.Cookies.Add(cookie); 

        SessionPersistence类使用微软.NET框架组件的BinaryFormatter来串行化和并行化对话状态为二进制格式以提供最佳性能。结果的二进制对话数据接着作为图象字段类型被存入SQL Server。 
    public mySession LoadSession(string key, string dsn,  
    int SessionExpiration) 

     SqlConnection conn = new SqlConnection(dsn); 
     SqlCommand LoadCmd = new SqlCommand(); 
     LoadCmd.CommandText = command; 
     LoadCmd.Connection = conn; 
     SqlDataReader reader = null; 
     mySession Session = null;  try 
     { 
      LoadCmd.Parameters.Add("@ID", new Guid(key)); 
      conn.Open(); 
      reader = LoadCmd.ExecuteReader(); 
      if (reader.Read()) 
      { 
       DateTime LastAccessed =reader.GetDateTime(1).AddMinutes(SessionExpiration); 
       if (LastAccessed >= DateTime.Now) 
        Session = Deserialize((Byte[])reader["Data"]); 
      } 
     } 
     finally 
     { 
      if (reader != null) 
       reader.Close(); 
      if (conn != null) 
       conn.Close(); 
     }  return Session; 

    private mySession Deserialize(Byte[] state) 

     if (state == null) return null;  mySession Session = null; 
     Stream stream = null;  try 
     { 
      stream = new MemoryStream(); 
      stream.Write(state, 0, state.Length); 
      stream.Position = 0; 
      IFormatter formatter = new BinaryFormatter(); 
      Session = (mySession)formatter.Deserialize(stream); 
     } 
     finally 
     { 
      if (stream != null) 
       stream.Close(); 
     } 
     return Session; 

      
      

  5.   

    在请求的末尾,Page类的Unload事件被启动了,一个同Unload事件一起注册的事件处理方法将串行化对话数据为二进制格式并将结果二进制数据存入SQL Server。 
    private void PersistSession(Object obj, System.EventArgs arg) 
    { sessionPersistence.SaveSession( 
      Server.UrlDecode(cookie.Value).ToLower().Trim(), dsn, Session, IsNewSession); 

    public void SaveSession(string key, string dsn,  
    mySession Session, bool IsNewSession) 

     SqlConnection conn = new SqlConnection(dsn); 
     SqlCommand SaveCmd = new SqlCommand();  
     SaveCmd.Connection = conn;  try 
     { 
      if (IsNewSession) 
       SaveCmd.CommandText = InsertStatement; 
      else 
       SaveCmd.CommandText = UpdateStatement;   SaveCmd.Parameters.Add("@ID", new Guid(key)); 
      SaveCmd.Parameters.Add("@Data", Serialize(Session)); 
      SaveCmd.Parameters.Add("@LastAccessed", DateTime.Now.ToString());   conn.Open(); 
      SaveCmd.ExecuteNonQuery(); 
     } 
     finally 
     { 
      if (conn != null) 
       conn.Close(); 
     } 

    private Byte[] Serialize(mySession Session) 

     if (Session == null) return null;  Stream stream = null; 
     Byte[] state = null;  try 
     { 
      IFormatter formatter = new BinaryFormatter(); 
      stream = new MemoryStream(); 
      formatter.Serialize(stream, Session); 
      state = new Byte[stream.Length]; 
      stream.Position = 0; 
      stream.Read(state, 0, (int)stream.Length); 
      stream.Close(); 
     } 
     finally 
     { 
      if (stream != null) 
       stream.Close(); 
     } 
     return state; 
    }     SessionPage类以及与它相关的类被封装在SessionUtility组件中。在一个新ASP.NET项目中,需要作SessionUtility组件的引用,为了与传统的ASP代码共享对话,每个页面由SessionPage代替Page类衍生出来。一旦移植完成了,新应用程序能通过说明SessionPage类中定义的对话变量切换回使用原来的HttpSession对象来显示基本的HttpSession。 
      

  6.   

    现在的问题是:有一使用cgi链接进行通行证验证的站点a;
    我现在是站点b,不明白 的是我如何知道用户没有在站点a进行登陆?