ASPNET内置身份认证,用户身份串门
我最近在一个系统中采用了ASPNET2.0内置的安全认证架构,测试时很正常,项目上线后发现一个严重的问题,就是A用户登录以后,在浏览过程中,有时候莫名奇妙的变成了B用户,显示的是B用户的信息,刷新一下,又变回A了,出现的次数,还是比较频繁的。麻烦帮我看看是什么原因?
下面把我的主要程序代码列一下,麻烦您看一下:
Web.config
<connectionStrings>
<add name="Evaluation" connectionString="Data Source=.;Initial Catalog=Evaluation;User ID=sa;Password=" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="Application" value="Evaluation" />
</appSettings>
<system.web>
<httpRuntime enableKernelOutputCache="false" maxRequestLength="1024"/>
<authentication mode="Forms">
<forms name="evaluation.lj.com" loginUrl="~/Login.aspx" protection="All" domain=".lj.com"/>
</authentication>
<machineKey
validationKey="282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760ADF21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141"
decryptionKey="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099" validation="SHA1"/>
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="Evaluation" applicationName="Evaluation"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed"
minRequiredPasswordLength="3" minRequiredNonalphanumericCharacters="0"/>
</providers>
</membership>
<roleManager enabled="true" cacheRolesInCookie="true" cookieName=".ASPROLES" defaultProvider="SqlProvider">
<providers>
<add connectionStringName="Evaluation" applicationName="Evaluation" name="SqlProvider" type="System.Web.Security.SqlRoleProvider"/>
</providers>
</roleManager>
<anonymousIdentification enabled="true"/>
说明,
1、其实是有两个项目,采用同样的machineKey设置来实现单点登录;
2、虽然设置了forms的name,但是因为在局域网运行,实际站点没有域名,是IP地址;
login.aspx
if (Membership.ValidateUser(username, password))
{
FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(240), true, "",
FormsAuthentication.FormsCookiePath);
string HashTicket = FormsAuthentication.Encrypt(Ticket);
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
if (Ticket.IsPersistent)
{
UserCookie.Expires = Ticket.Expiration;
}
//生成Cookie
Response.Cookies.Add(UserCookie); //输出Cookie
}
在网上找到一篇文章,好像和我的情况有些类似,
http://www.cnblogs.com/niuniu502/archive/2008/04/01/1132287.html
但是我的登录界面不是ajax的,包含到iframe中,也没有解决问题;
微软的文章
http://support.microsoft.com/kb/917072
正象第一篇文章作者提到的那样,“实际上KernelOutputCache被禁用后,COOKIES依然会被分发到不同的用户主机上,也就是说所谓的MS的解决方案对偶一点用页没”。