.net Forms身份验证不能用在应用的分布式部署中吗?
参照网上的一些方法,使用Forms身份验证对应用进行分布式部署,发现没有成功。
应用部署的两台内网服务器:192.168.1.19,192.168.1.87,使用Nginx做负载分配,配置完全相同;每台都可以登录运行。
<system.web>
<!--配置 ASP.NET 使用的安全身份验证模式,以标识传入的用户。domain=".zt-express.com" -->
<authentication mode="Forms">
<forms name=".GDZDAUTHENFORMS" loginUrl="~/Login.aspx" timeout="" defaultUrl="~/Main.aspx" path="/" protection="All" />
</authentication>
<machineKey validationKey="E804106B394DE7148524A5FB0E7E282F05C3BB98553931F2B3FCDC896473390205326A876AA5490050D795FA181604651878B4285475150437A73F9D705E412A" decryptionKey="9BE9F489677A8285D6A00E902857ABB2986C73534FF2A901" validation="SHA1" />
<authorization>
<allow users="*" />
</authorization>
<anonymousIdentification enabled="true" cookieName=".GDZDanonymous" />
<httpRuntime />
<compilation debug="true" targetFramework="4.0" />
<pages enableSessionState="true" controlRenderingCompatibilityVersion="4.0" />
<customErrors mode="Off" />
<sessionState timeout="">
</sessionState>
</system.web>
以下时登录成功后的处理
/// <summary>
/// 创建一个票据,放在cookie中
/// 票据中的数据经过加密,解决一下cookie的安全问题。
/// </summary>
/// <param name="userInfo">登录用户</param>
/// <param name="issueDateTime">发布时间</param>
/// <param name="experation">过期时间</param>
/// <param name="isPersistent">持久性</param>
public static void SetCookie(BaseUserInfo userInfo, DateTime? issueDateTime = null, DateTime? experation = null, bool isPersistent = true)
{
if (issueDateTime == null)
{
issueDateTime = DateTime.Now;
}
if (experation == null)
{
//设置COOKIE过期时间
experation = DateTime.Now.AddHours(SystemInfo.UserLoginExperation);
}
BaseSystemInfo.UserInfo = userInfo;
BaseSystemInfo.UserInfo.ServicePassword = BaseSystemInfo.ServicePassword;
BaseSystemInfo.UserInfo.ServiceUserName = BaseSystemInfo.ServiceUserName;
BaseSystemInfo.UserInfo.SystemCode = BaseSystemInfo.SystemCode;
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string userData = javaScriptSerializer.Serialize(BaseSystemInfo.UserInfo);
//生成验证票据,其中包括用户名、生效时间、过期时间、是否永久保存和用户数据等。
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(, userInfo.NickName, (DateTime)issueDateTime, (DateTime)experation, isPersistent, userData, FormsAuthentication.FormsCookiePath);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
cookie.Expires = (DateTime)experation;
HttpResponse response = HttpContext.Current.Response;
//指定客户端脚本是否可以访问[默认为false]
cookie.HttpOnly = true;
//指定统一的Path,比便能通存通取
cookie.Path = "/";
response.AppendCookie(cookie); //移除一下权限缓存数据 以便重新获取缓存数据
RemoveRedisCache(userInfo);
}
以下是验证的代码
//测试 HttpContext.Current.User.Identity.IsAuthenticated在分布式部署中是否有效
Response.Write(string.Format("测试 HttpContext.Current.User.Identity.IsAuthenticated在分布式部署中是否有效IsAuthenticated:{0}", HttpContext.Current.User.Identity.IsAuthenticated));
Response.Write("<br/>cookie输出开始=============================");
foreach (string cookieName in Request.Cookies)
{
var mycookie = Request.Cookies[cookieName];
if (mycookie != null)
{
Response.Write("<br/>" + cookieName + "中含有" + mycookie.Values.Count + "个Key");
if (mycookie.Values.Count > )
{
foreach (string s in mycookie.Values)
{
Response.Write("<br/> “" + s + "”=" + mycookie[s].ToString() + ";");
}
}
}
}
Response.Write("<br/>cookie输出完毕============================="); Response.Write("<br/>FormsCookieName=" + FormsAuthentication.FormsCookieName);
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null)
{
string userData = authTicket.UserData;
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
var userInfo = javaScriptSerializer.Deserialize<BaseUserInfo>(userData);
Response.Write("<br/>NickName=" + userInfo.NickName);
}
else
{
Response.Write("<br/>authTicket = null");
}
}
else
{
Response.Write("<br/>authCookie = null");
}
Response.Write("<br/>ClientIpAddress = " + UserInRedis.GetCurrentIpAddress(HttpContext.Current));
Response.Write("<br/>ServerIpAddress = " + Request.ServerVariables.Get("Local_Addr"));
部署完毕,登录系统后,访问测试页面
可以看到当前访问应用被分配到192.168.1.19上了
现在把192.168.1.19的应用停掉,再来访问测试页面
从上面可以看出,访问被分配到192.168.1.87上了,而用于认证的.GDZDAUTHENFORMS cookie没有传过来,其它的cookie传过来了。
这是什么原因呢?同样的域名应该说cookie会传到后台的啊。
参考:http://www.cnblogs.com/fish-li/archive/2012/04/15/2450571.html等文章还是没实现,服务器配置是一样的,按理说,同样的域名,访问时应该把cookie都带过去的啊。
同样的访问请求,应该说每次都会带cookie的,为何在分布式部署中,指向另外一台机子时,cookie获取不到
此问题已发到msdn:https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/f666f1d1-3d9e-4620-babb-1eea9302c0d9/forms?forum=295
.net Forms身份验证不能用在应用的分布式部署中吗?的更多相关文章
- ASP.NET Forms 身份验证
ASP.NET Forms 身份验证 在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数.2. 创建登录页. 登录页中的操作包括: 1. 验证用 ...
- asp.net Forms身份验证
Web.config中的配置<system.web><authentication mode="Forms"> <forms name="K ...
- 采用Asp.Net的Forms身份验证时,持久Cookie的过期时间会自动扩展
原文:http://www.cnblogs.com/sanshi/archive/2012/06/22/2558476.html 若是持久Cookie,Cookie的有效期Expiration属性有当 ...
- 采用Asp.Net的Forms身份验证时,非持久Cookie的过期时间会自动扩展
问题描述 之前没有使用Forms身份验证时,如果在登陆过程中把HttpOnly的Cookie过期时间设为半个小时,总会收到很多用户的抱怨,说登陆一会就过期了. 所以总是会把Cookie过期时间设的长一 ...
- Web分布式部署,跨应用程序Forms身份验证的集成方案
最近一个项目要求进行分布式部署.保证在双十一期间系统的正常运行,虽然该系统平时访问量不是很大,但是基于业务需要,必须在至少两台服务器上部署. 该系统需要登录后才可以使用,首先需要解决分布式部署的用户状 ...
- Forms身份验证和基于Role的权限验证
Forms身份验证和基于Role的权限验证 从Membership到SimpleMembership再到ASP.NET Identity,ASP.NET每一次更换身份验证的组件,都让我更失望.Memb ...
- ASP.NET:Forms身份验证和基于Role的权限验证
从Membership到SimpleMembership再到ASP.NET Identity,ASP.NET每一次更换身份验证的组件,都让我更失望.Membership的唯一作用就是你可以参考它的实现 ...
- asp.net的forms身份验证 单用户身份验证
asp.net的forms身份验证 单用户身份验证 首先要配置Web.config文件 <system.web> <authentication mode="Forms& ...
- 通过Forms身份验证设置不同页面的访问权限
使用Forms身份验证的时候,如果允许注册页面可以匿名用户访问,其他所有页面只允许注册用户访问,我们可以如下设置web.config文件来达到上述的效果: 1.在“system.web”节点下,添加登 ...
随机推荐
- RSA加密算法深入篇
如果你问我,哪一种算法最重要? 我可能会回答"公钥加密算法". 因为它是计算机通信安全的基石,保证了加密数据不会被破解.你可以想象一下,信用卡交易被破解的后果. 进入正题之前,我先 ...
- 《吃了么》Alpha版本发布说明
1.功能描述 功能一 查询周边餐厅 选中一条后,会跳转到相应餐厅的网址,获取具体的信息: 功能二 查询指定地点地餐厅 功能三 查询菜谱 具体的菜谱: 还有收藏夹的功能,点击标题栏的心形图标可以将菜 ...
- Linux基础实践
Linux基础实践 1.1 应用安装 要求:掌握软件源的维护方法,配置系统使用软件源镜像.掌握通过软件源来查找,安装,卸载,更新软件的方法 备份原地址列表文件:sudo cp /etc/apt/sou ...
- oracle加注释
COMMENT ON table GC_G_DOC84 IS '行政处罚撤销决定书'; COMMENT ON column GC_G_DOC84.CASEID IS '案件记录ID';
- js实现树形内容展示
1.首先这里有一个demo,里边有封装好的js文件.地址:http://files.cnblogs.com/files/feifeishi/dtree.zip 2.直接上代码 <div styl ...
- 第三个Sprint冲刺第4天
成员:罗凯旋.罗林杰.吴伟锋.黎文衷 讨论内容:各成员汇报各自完成的情况.
- 第三个spring冲刺第3天
基本功能跟界面都完成了,今天小组开了个会,基于跟别的小组对比的效果,感觉自己组的效果没别人的好,很多方面还欠缺,所以我们会继续跟进完善.
- 【SE】Week3 : 四则运算式生成评分工具Extension&Release Version(附加题)
[附加题]第四阶段目标 - 界面模块,测试模块和核心模块的松耦合. 写到这里我只想吐槽一句,哪天我能写出功能复杂且真正松耦合的模块,我应该就不用写代码了吧[手动再见.. 当然这只是强调下松耦合和代码复 ...
- React16新特性
React的16版本采用了MIT开源许可证,新增了一些特性. Error Boundary render方法新增返回类型 Portals 支持自定义DOM属性 setState传入null时不会再触发 ...
- spring mvc的工作原理
该文转载自:http://blog.csdn.net/u012191627/article/details/41943393 SpringMVC框架介绍 1) spring MVC属于SpringFr ...