62,267
社区成员
发帖
与我相关
我的任务
分享
//为了测试,改成了1分钟
<forms name="ao"
loginUrl="login.aspx"
timeout="1"
cookieless="UseDeviceProfile"
protection="All"
defaultUrl="Account/default.aspx" />
</authentication>
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
//如果一开始能跳转到受保护的页面,说明持久化cookie起作用
Response.Redirect("Account/default.aspx");
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (FormsAuthentication.Authenticate(txtUserName.Text, txtUserPasswork.Text))
{
HttpCookie cookie = FormsAuthentication.GetAuthCookie(txtUserName.Text, true);
cookie.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(cookie);
Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserName.Text, true));
}
else
{
throw new Exception("登录失败!");
}
}