单一站点下实现验证登录我是:web.config中<appSettings>
<add key="TestConnStr" value="***********连接******** "/>
<add key="DomainStr" value="192.168.1.18"/>
</appSettings><authentication mode="Forms">
<forms name="test.MyCookie" loginUrl="Index.aspx" timeout="20" protection="All">
<credentials passwordFormat="SHA1">
</credentials>
</forms>
</authentication>
<machineKey validationKey="*************" decryptionKey="*************"/>
<authorization>
<allow users="*"/>
</authorization>-------------------------------------------------------------然后自己写一个类,类中
添加cookie
 public static void AddCookie(string UserID)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, UserID, DateTime.Now, DateTime.Now.AddMinutes(30), false, UserID, FormsAuthentication.FormsCookiePath);
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
            HttpCookie cookie2 = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            string Domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            if (Domain.IndexOf("192.168.1.18") > 0)
            {
                cookie.Domain = ConfigurationManager.AppSettings["DomainStr"];
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
删除cookiepublic static void RemoveCookie()
        {            FormsAuthentication.SignOut();            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
           
            string Domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            if (Domain.IndexOf("192.168.1.18") > 0)
            {
                cookie.Domain = ConfigurationManager.AppSettings["DomainStr"];
            }
            cookie.Expires = System.DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
       }
获取cookiepublic static string GetCookie()
        {            try
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                if (authCookie != null)
                {
                    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                    string MyCookie = authTicket.UserData.ToString();
                    return MyCookie;
                }
                else
                    return "0";
            }
            catch
            {
                return "0";
            }        }
如此单一站点下完全可以解决,但现在要跨站点登录,比如登录 www.aaa.com后就能 登录 xxx.bbb.com,www.ccc.com 于是我改了web.config. 
加了<add key="DomainStr2" value="192.168.1.7"/>类中添加cookie的方法也改成:public static void AddCookie(string UserID)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, UserID, DateTime.Now, DateTime.Now.AddMinutes(30), false, UserID, FormsAuthentication.FormsCookiePath);
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
            HttpCookie cookie2 = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            string Domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
            if (Domain.IndexOf("192.168.1.18") > 0 || Domain.IndexOf("192.168.1.7") > 0)
            {
                cookie.Domain = ConfigurationManager.AppSettings["DomainStr"];
                cookie2.Domain = ConfigurationManager.AppSettings["DomainStr2"];
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
            HttpContext.Current.Response.Cookies.Add(cookie2);
        }删除cookie也进行了类似的修改,但貌似不行。希望大侠们给我看看....该怎么写? 谢谢了!!!
 

解决方案 »

  1.   

    第一个添加方法中HttpCookie cookie2 = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
     
    是多余的,复制时没删掉...
      

  2.   

    跨站点使用forms身份验证好像必须得在两个站点分别添加票证吧
      

  3.   


    大哥 能说的详细点吗?我在 另一个站点比如:192.168.1.7的web.config中也有相同的
    <machineKey validationKey="**************" decryptionKey="*************"/>2个站点的这个机器码是相同的。还是什么????????????????
      

  4.   

    我之前做跨站点forms身份验证的时候是这么处理的,不知道对不对,但能实现:
    比如A站点为主站,B为子站
    A,B均为forms身份验证,那么登录页面只做一个,放在A站点
    当在A站登录时,很自然的在A站添加当然登录用户的票证
    然后当B站需要登录的时候,跳转到A站的登录页面,先判断此用户在A站是否已登录,如果未登录则在A站添加票证,然后返回结果给B站点,B站点得到结果后再在B站点添加此用户的票证,这样一来两个站点都登录成功了
    退出的时候也是用的这种方法
    此方法是我自己想出来的,不一定正确,你可以参考
      

  5.   


    大哥,也就是  我的那种,当登录 A站点时 添加 A站点的cookie,同时也添加 B站的cookie的思路不合理,对吗 ?
      

  6.   

    也不是说不合理,你这种想法是正确的,就是要两边同时添加cookie,你的问题是出现在跨域添加cookie上,这个问题在网上能找到资料的,我就帮不了你了
      

  7.   


    就算我回传一个参数 到 B站点, B站点又怎么添加cookie???????????????????
      

  8.   


    传用户的ID 过去?...如此B站点要登录 就必须到A站点下登录?----〉然后跳转回历史页,然后再跳转到A的登录页判断是否登录?,因为之前登录了 所以再跳回B站点???
    ???????????????????不是吧,跨站点这么做?
      

  9.   


    我的跨站点做法 和你差不多 不过我的其他网站是二级域名
    验证cookie就是一个 
    还有一种思路是 a站点登陆成功 通过form传值 登录b站点 想让它不知道 可以用
    <iframe src="" width="0" height="0"></iframe>
      

  10.   


    二级域名 也样的啊。验证cookie就是一个 ?????????????
    大哥你是怎么验证的啊??指点迷津啊。。
      

  11.   

    二级域名 共用cookie 你到网上搜吧 那上面连做法都有了
      

  12.   


    大哥,你的意思 是不是 这些站点在同一服务器上,比如:www.aaa.com , bbb.aaa.com, ccc.aaa.com
      

  13.   

    不一定在同一个 服务器上 
    www.aaa.com , bbb.aaa.com, ccc.aaa.com 
    必须的 
    cookies的domain设置为".aaa.com"
    就可以了
      

  14.   


    如果 cookies的domain设置为" 一个IP地址 "   就不可以了???
      

  15.   

    这个问题我也一直没解决。<authentication mode="Forms">
        <forms name=".ARTAUTH" 
            loginUrl="Login.aspx" 
            timeout="90" 
            slidingExpiration="true" 
            cookieless="UseCookies" 
            protection="All" 
            requireSSL="false" 
            defaultUrl="~/Default.aspx" 
            path="/" 
            enableCrossAppRedirects="false"
     
            domain=""/>
    </authentication>这里的enableCrossAppRedirectsdomain,一直是个疑惑。一下,老早就想解决这个问题了。
      

  16.   

    那你应该理解下 域名和主机的区别 
    http://topic.csdn.net/t/20020404/12/621966.html
      

  17.   

    这个讲的更清楚 不过我打不开 
    http://www.5bay.cn/blog/read.php/264.htm
      

  18.   

    如果是 登录www.aaa.com站点  然后连接到 www.bbb.com站点 同样登录。该怎么搞呢?
      

  19.   

    刚刚说的 用form 传值 就可以 
    不过 这个 好像非正常 退出时 用户不同步
    有时会出现a站点还是登陆状态 b站点是离线状态