参照网上的一些方法,使用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="2880" 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="3600"></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(1, 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 > 0){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身份验证不能用在应用的分布式部署中吗?相关推荐

  1. Forms身份验证基本原理

    要采用Forms身份验证,先要在应用程序根目录中的Web.config中做相应的设置: <authentication mode="forms">     <fo ...

  2. 为不同目录设置Forms身份验证

    在进行Forms身份验证时,如果采用如下的方法配置web.config的话,则会出现所有的页面都要进行验证,这是用户所不能容忍的,具体代码如下: 1<authentication mode=&q ...

  3. [转][.NET 基于角色安全性验证] 之三:ASP.NET Forms 身份验证

    在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数. 2. 创建登录页. 登录页中的操作包括: 1. 验证用户名和密码是否正确. 2. 创建身份 ...

  4. .NET Forms身份验证

    .NET表单身份验证 ASP.NET Forms 身份验证的简单实现:1)在Web.config文件中配置应用程序使用 Forms 身份验证:2)创建登陆页面,将用户身份验证票证添加到Cookie集合 ...

  5. 实现基于 ASP.NET Forms 身份验证的跨子域单点登录

    对于跨应用程序的 ASP.NET Forms 身份验证,相信大家应该都不陌生,几年前很多文章都介绍了如何实现,比如 MSDN 的 跨应用程序进行 Forms 身份验证,唐朝程序员 的 ASP.NET站 ...

  6. asp.net Forms身份验证

    Web.config中的配置 <system.web> <authentication mode="Forms"> <forms name=" ...

  7. forms身份验证 不跳转_Django用户身份验证实战

    在这篇Django文章中,我们 将讨论Django User 验证,Django附带了一个用户认证系统.它处理用户帐户,组,权限和基于cookie的用户会话.Django身份验证系统同时处理身份验证和 ...

  8. IE11下用forms身份验证的问题

    <authentication mode="Forms"><forms name="weboa" loginUrl="login.a ...

  9. forms 身份验证(授权)详解

    首先在 web.config 中设置 <authentication mode="Forms">设置 mode="Forms"   <form ...

最新文章

  1. 架构师养成之道-02-jvm原理
  2. Flume的Channel
  3. 点击打印出现IE已经阻止此站点用不安全方式使用 ActiveX 控件解决方
  4. 机器学习 对模型进行惩罚_使用Streamlit对机器学习模型进行原型制作
  5. python函数封装计算n运算_在Python里面怎么可以运算出999999999**999999999,求思路?...
  6. linux 测试cpu计算圆周率_Linux下测试CPU性能
  7. Oracle删除用户与删除表
  8. CCNA——网络初认识
  9. CSDN创作的markdown语法效果示意图
  10. 成外集训小记(更新到7.31)
  11. java retainall源码,Java CopyOnWriteArraySet retainAll()用法及代码示例
  12. ResNet网络结构代码该怎么看
  13. 惠普计算机安转不上xp,雨林木风xp系统上安装不上惠普打印机驱动的解决办法...
  14. 3D建模软件快捷键操作:3DMAX篇(第二期)
  15. 使用JS数组迭代方法渲染页面数据并实现查询功能
  16. Bose SoundLink Revolve或者Bose SoundLink Revolve+ AUX音频播放一会没有声音
  17. 杨百万:调整后最值得关注的10大金股
  18. Unity模糊远处物体,近处清晰,景深效果
  19. 无法打开网页版晓木虫怎么办?
  20. 基于stm32的无人机控制系统设计

热门文章

  1. 从内存细看static
  2. Maven自動化構建工具
  3. mysql的结构,段页区,及客户端命令
  4. RT ROM boot简介
  5. 把报表的数据导出Excel
  6. 漫谈词向量之基于Softmax与Sampling的方法
  7. 五大存储模型关系模型、键值存储、文档存储、列式存储、图形数据
  8. What’s the Difference between a Hue, Tint, Shade and Tone ?
  9. Git pull[push] 不用每次输入用户名和密码
  10. Android自定义波浪加载圆形进度条——(自定义控件 一)