以下是登陆页面中的代码:
if ((new UserSystem()).GetUserByUserName(Tx_Account.Text, password))
{
int RightNumber=(new UserSystem()).GetUserRightNumber(Tx_Account.Text);
FormsAuthenticationTicket ticket=new FormsAuthenticationTicket(
1, Tx_Account.Text, DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
RightNumber.ToString()); string CookieStr=FormsAuthentication.Encrypt(ticket);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,CookieStr);
cookie.Path=FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage("*", false);
}
else
{
MismatchLabel.Visible = true;
} 登陆成功以后,我的程序在另一个页面的Page_Load()中读出cookie中的FormsAuthenticationTicket读出FormsAuthenticationTicket的测试代码如下:
string hhh=Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value;
FormsAuthenticationTicket Ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);string username=Ticket.Name;
string haha=Ticket.UserData;我跟踪执行了整个过程,如果我用admin登陆, 写入的cookie的ticket的name=admin, UserData=5
但是我读出cookie之后还原成FormsAuthenticationTicket 发现Ticket的name=* UserData=null(空)真不知道这是怎么回事!
if ((new UserSystem()).GetUserByUserName(Tx_Account.Text, password))
{
int RightNumber=(new UserSystem()).GetUserRightNumber(Tx_Account.Text);
FormsAuthenticationTicket ticket=new FormsAuthenticationTicket(
1, Tx_Account.Text, DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
RightNumber.ToString()); string CookieStr=FormsAuthentication.Encrypt(ticket);
HttpCookie cookie=new HttpCookie(FormsAuthentication.FormsCookieName,CookieStr);
cookie.Path=FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage("*", false);
}
else
{
MismatchLabel.Visible = true;
} 登陆成功以后,我的程序在另一个页面的Page_Load()中读出cookie中的FormsAuthenticationTicket读出FormsAuthenticationTicket的测试代码如下:
string hhh=Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value;
FormsAuthenticationTicket Ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);string username=Ticket.Name;
string haha=Ticket.UserData;我跟踪执行了整个过程,如果我用admin登陆, 写入的cookie的ticket的name=admin, UserData=5
但是我读出cookie之后还原成FormsAuthenticationTicket 发现Ticket的name=* UserData=null(空)真不知道这是怎么回事!
cookie.Expires =DateTime.Now.Add(TimeSpan.FromDays(10));
保证10天不过期
.net的session不稳定,难道cookie也不稳定?
真不知道验证应该用什么机制了?
using System.Web.Security;
登陆:FormsAuthentication.SetAuthcookie or FormsAuthentication.RedirectFromLoginPage
获取用户名:HttpContext.Current.User.Identity.Name
}
else
{
MismatchLabel.Visible = true;
}
你的这个方法用的有问题,看看这个方法的参数用法,第一个参数是用户名称,所以你的用户名称是
* 而用户数据没有 不要用这个方法 在Web.config文件和Global.asax中设置好后直接可以用Response.Redirect()
方法转移。