关于asp.net FormsAuthenticationTicket 的问题

来源:百度知道 编辑:UC知道 时间:2024/07/07 09:14:11
网站页面是用FormsAuthenticationTicket进行验证的,没有用角色管理,在内网测试没有一切没有问题,不过用google代理登录就出现问题了,总是登录不上
代码是下面这段:
bool islogin=false;
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
isLogin = true;

FormsIdentity formsId = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket authTicket = formsId.Ticket;
if (authTicket != null)
{
userId = int.Parse(authTicket.Name);
userrole = int.Parse(authTicket.UserData);
}
}
}

return islogin;
返回的肯定一直是false,就前两个if里面有问题,
感觉像是HttpContext.Current.User这个的问题,还是cookie的问题?有哪位大哥遇到过这种问题?指点一下,

构建基于forms的验证机制过程如下:
1,设置IIS为可匿名访问和asp.net web.config中设置为form验证
2,检索数据存储验证用户,并检索角色(如果不是基于角色可不用)
3,使用FormsAuthenticationTicket创建一个Cookie并回发到客户端,并存储
角色到票据中,如:
FormsAuthentication.SetAuthCookie(Username,true | false)
cookies保存时间:
HttpContext.Current.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.Now.AddDays(1)

如果需要存储角色,采用:
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(
1, // 版本号。
txtUserName.Text, // 与身份验证票关联的用户名。
DateTime.Now, // Cookie 的发出时间。
DateTime.Now.AddMinutes(20),// Cookie 的到期日期。
false, // 如果 Cookie 是持久的,为 true;否则为 false。
roles ); // 将存储在 Cookie 中的用户定义数据。
roles是一个角色字符串数组
string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //加密

存入Cookie
HttpCookie authCookie =
new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);

Response.Cookies.Add(authCookie);