本来公司有域控服务器,大多数用户通过域登录电脑,这样系统用户验证这一块无需理会,可是个人电脑也很多,不接受域管理,没办法了,系统都采用了Form验证的方式。
其实现实起来非常简单,原理都知道,可在实际使用过程当中出现的问题也很多。所以特将一些遇到的问题,涉及到的代码贴出,以方便日后查询。
产生票据,如果生成Cookie必须要设置Expiration,可以继承票据里的Expiration,但是也必须加入,否者不生成Cookie,目前还不清楚到底为什么。
newUserCookie.Expires
=
newTicket.Expiration;
验证成功后,URL转向也要更改,如下:
//
采用这样方式
Response.Redirect(FormsAuthentication.GetRedirectUrl(newTicket.Name, newTicket.IsPersistent));
// 不能采取以下方式
// FormsAuthentication.RedirectFromLoginPage(newTicket.Name, newTicket.IsPersistent);
Response.Redirect(FormsAuthentication.GetRedirectUrl(newTicket.Name, newTicket.IsPersistent));
// 不能采取以下方式
// FormsAuthentication.RedirectFromLoginPage(newTicket.Name, newTicket.IsPersistent);
WebConfig中也需要一样,加密方式,Cookie名称之类的。
<
authentication mode
=
"
Forms
"
>
< forms name = " .AspxCookieNames " protection = " All " timeout = " 60 " loginUrl = " Login.aspx " defaultUrl = " default.aspx " />
</ authentication >
< machineKey validationKey = " pokiu85CB121726BEAA132A01A640D815C5D02A47C7polll75D62AE62B6F3AC31FC26068CC248DB821BC74BA8A22516FF2197823B3C839432A1D588B " decryptionKey = " 340A379lo09o1B4F7A72569p566FC281 " validation = " SHA1 " />
< authorization >
< deny users = " ? " />
< allow users = " * " />
</ authorization >
< forms name = " .AspxCookieNames " protection = " All " timeout = " 60 " loginUrl = " Login.aspx " defaultUrl = " default.aspx " />
</ authentication >
< machineKey validationKey = " pokiu85CB121726BEAA132A01A640D815C5D02A47C7polll75D62AE62B6F3AC31FC26068CC248DB821BC74BA8A22516FF2197823B3C839432A1D588B " decryptionKey = " 340A379lo09o1B4F7A72569p566FC281 " validation = " SHA1 " />
< authorization >
< deny users = " ? " />
< allow users = " * " />
</ authorization >