FormsAuthentication如何添加过期时间

protected void btnLogin_Click(object sender, EventArgs e)
    
{
        
string userName = "UserName";
        
string password = "Password";
        
if (txtUserName.Text == userName && txtPassword.Text == password)
        
{
            setValidTime(chkRememberMe.Checked, userName);
            Response.Write(
"<script>top.location='Default.htm';</script>");
        }

        
else
        
{
            labMsg.Text 
= "登录失败,请重试";
        }

    }


    
/**//// <summary>
    
/// 设置有效时间
    
/// </summary>

    protected void setValidTime(bool RememberMe, string strUserName)
    
{
        
if (RememberMe)
        
{
            FormsAuthentication.SetAuthCookie(strUserName, 
true);
            HttpCookie lcookie 
= Context.Response.Cookies[FormsAuthentication.FormsCookieName];
            lcookie.Expires 
= DateTime.Now.AddYears(1);
        }

        
else
        
{
            FormsAuthentication.SetAuthCookie(strUserName, 
false);
        }

    }

注销

    
protected void btnSignOut_Click(object sender, EventArgs e)
    
{
        FormsAuthentication.SignOut();
    }
posted @ 2007-09-11 08:42  么么茶.NET  阅读(1065)  评论(0编辑  收藏  举报