关于ASP.NET的Form认证机制遇到的一些问题!
Web.Config配置如下:<authentication mode="Forms">
<forms name=".UIASAuth" loginUrl="Login.aspx" defaultUrl="FrmMain.aspx" path="/" timeout="20">
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
程序后台代码如下所示:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, //版本号
this.txtUserID.Text, //登录的用户名
DateTime.Now, //票劵发布时间
DateTime.Now.AddMinutes(10), //票劵过期时间
false, //是否在关闭浏览器后仍然保留登录信息
"this is testpage", //可加入少量用户数据(注意:不能为 null)
FormsAuthentication.FormsCookiePath //cookie 路径
);
string encryptedTicket = FormsAuthentication.Encrypt(ticket); //加密
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
DateTime now = DateTime.Now;
authCookie.Value = now.ToString();
authCookie.Expires = now.AddHours(10);
Response.Cookies.Add(authCookie);
Response.Redirect("FrmMain.aspx");
但是,出现的情况是,不会转到Response.Redirect("FrmMain.aspx");页面仍停留在登录界面。
另外,不会产生Cookie文件,而使用FormsAuthentication.SetAuthCookie(this.txtUserID.Text, true);则可以。
这是什么原因呢? --------------------编程问答-------------------- 先set,在转向吧
FormsAuthentication.SetAuthCookie(this.txtUserID.Text, true);
Response.Redirect("FrmMain.aspx");
不过,一般我都是这样写的
Response.Redirect(FormsAuthentication.GetRedirectUrl(this.txtUserID.Text, true));
--------------------编程问答-------------------- 嗯,1楼的是正确的,要先设置一下通过验证才能跳转过去,不然会一直在登录页面 --------------------编程问答-------------------- HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
DateTime now = DateTime.Now;
authCookie.Value = now.ToString();
authCookie.Expires = now.AddHours(10);
Response.Cookies.Add(authCookie);
在客户端差生Cookies,内容包括失效时间等信息。
不过再使用FormsAuthentication.SetAuthCookie(),将会改变Cookies的内容,个人觉得之前的工作没意义了。
补充:.NET技术 , ASP.NET