服務(wù)熱線
153 8323 9821
HttpContext.Current.User.Identity.IsAuthenticated=false;
HttpContext.Current.User.Identity.Name==""
解釋:當(dāng)用戶登錄時(shí),服務(wù)器為確認(rèn)客戶端通過(guò)驗(yàn)證要通過(guò)cookie向客戶端寫驗(yàn)證(Authenticat)信息,在登錄頁(yè)面剛驗(yàn)證完成后服務(wù)器 還沒有把cookie 回發(fā)到Client,所以會(huì)沒有值,當(dāng)服務(wù)器第二次Response的時(shí)候,就會(huì)從客戶端讀取Cookie,要想有此Cookie還要在 web.config文件中配置相應(yīng)的參數(shù)
<system.web>
<authentication mode="Forms">
<forms domain="bokoAdmin" timeout="20" loginUrl="Login.aspx" path="/"></forms>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
<system.web>
程序驗(yàn)證:
if (Membership.ValidateUser(tbx_username.Text.TrimEnd(), tbx_password.Text.TrimEnd()))
{
FormsAuthentication.SetAuthCookie(tbx_username.Text.TrimEnd(), true,FormsAuthentication.FormsCookiePath);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, tbx_username.Text, DateTime.Now, DateTime.Now.AddMinutes(20), false, tbx_username.Text);
// generate new identity
FormsIdentity identity = new FormsIdentity(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
// write to client.
Response.Cookies.Add(cookie);
}
其中加粗體為主要語(yǔ)句,有此一句就可以實(shí)現(xiàn)HttpContext.Current.User.Identity.IsAuthenticated=true;