单一站点下实现验证登录我是: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也进行了类似的修改,但貌似不行。希望大侠们给我看看....该怎么写? 谢谢了!!!
<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也进行了类似的修改,但貌似不行。希望大侠们给我看看....该怎么写? 谢谢了!!!
解决方案 »
- 【JS框架】Ext 和jquery框架的疑问....
- vs2008中断Debug后,却能提交数据
- .net 从数据库中读取图片的问题
- 为何在ASP.NET中将数组赋值给Excel工作表的单元格上不成功?
- 一个很简单的问题
- gridview 可以去掉标题栏吗?只有10分了。感谢回答
- 刷新问题
- Codebebind问题!请教高手!
- 客户端获取listbox当前选定的值
- 【,简单问题复杂化?????????】.......................
- 关于.NET丢失CSS样式的问题?
- 未能加载文件或程序集“EeekSoft.Web.PopupWin”或它的某一个依赖项。系统找不到指定的文件。急!
是多余的,复制时没删掉...
大哥 能说的详细点吗?我在 另一个站点比如:192.168.1.7的web.config中也有相同的
<machineKey validationKey="**************" decryptionKey="*************"/>2个站点的这个机器码是相同的。还是什么????????????????
比如A站点为主站,B为子站
A,B均为forms身份验证,那么登录页面只做一个,放在A站点
当在A站登录时,很自然的在A站添加当然登录用户的票证
然后当B站需要登录的时候,跳转到A站的登录页面,先判断此用户在A站是否已登录,如果未登录则在A站添加票证,然后返回结果给B站点,B站点得到结果后再在B站点添加此用户的票证,这样一来两个站点都登录成功了
退出的时候也是用的这种方法
此方法是我自己想出来的,不一定正确,你可以参考
大哥,也就是 我的那种,当登录 A站点时 添加 A站点的cookie,同时也添加 B站的cookie的思路不合理,对吗 ?
就算我回传一个参数 到 B站点, B站点又怎么添加cookie???????????????????
传用户的ID 过去?...如此B站点要登录 就必须到A站点下登录?----〉然后跳转回历史页,然后再跳转到A的登录页判断是否登录?,因为之前登录了 所以再跳回B站点???
???????????????????不是吧,跨站点这么做?
我的跨站点做法 和你差不多 不过我的其他网站是二级域名
验证cookie就是一个
还有一种思路是 a站点登陆成功 通过form传值 登录b站点 想让它不知道 可以用
<iframe src="" width="0" height="0"></iframe>
二级域名 也样的啊。验证cookie就是一个 ?????????????
大哥你是怎么验证的啊??指点迷津啊。。
大哥,你的意思 是不是 这些站点在同一服务器上,比如:www.aaa.com , bbb.aaa.com, ccc.aaa.com
www.aaa.com , bbb.aaa.com, ccc.aaa.com
必须的
cookies的domain设置为".aaa.com"
就可以了
如果 cookies的domain设置为" 一个IP地址 " 就不可以了???
<forms name=".ARTAUTH"
loginUrl="Login.aspx"
timeout="90"
slidingExpiration="true"
cookieless="UseCookies"
protection="All"
requireSSL="false"
defaultUrl="~/Default.aspx"
path="/"
enableCrossAppRedirects="false"
domain=""/>
</authentication>这里的enableCrossAppRedirects和domain,一直是个疑惑。一下,老早就想解决这个问题了。
http://topic.csdn.net/t/20020404/12/621966.html
http://www.5bay.cn/blog/read.php/264.htm
不过 这个 好像非正常 退出时 用户不同步
有时会出现a站点还是登陆状态 b站点是离线状态