配置相关
<authentication mode="Forms">
<forms
name=".ASPXAUTH"
loginUrl="login.aspx"
defaultUrl="UserCenter.aspx"
protection="All"
timeout="30"
path="/"
requireSSL="false"
slidingExpiration="false"
enableCrossAppRedirects="false"
cookieless="UseDeviceProfile"
domain=""
/>
</authentication>
登录代码
public void Logining()
{
var name = Request.Form["name"];
var pwd = Request.Form["pwd"];
BLLUser us = new BLLUser();
var ishave = us.IsHaveUser(name, pwd);
if (ishave != null)
{
//添加身份验证
FormsAuthentication.SetAuthCookie(ishave.UserName, true);
//设置票据
FormsAuthenticationTicket tic = new FormsAuthenticationTicket(1, ishave.UserName, DateTime.Now, DateTime.Now.AddSeconds(12), true, "User");
FormsAuthentication.Encrypt(tic);
HttpCookie ticketCookie = new HttpCookie("", FormsAuthentication.Encrypt(tic));
Response.Cookies.Add(ticketCookie);
Response.Redirect("UserCenter.aspx");
}
}
验证代码
public void IsLogin()
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
Response.Write("已登录" + HttpContext.Current.User.Identity.Name);
}
else
{
Response.Write("未登录");
FormsAuthentication.RedirectToLoginPage();
}
}