浏览量: 次
HttpContext.Current.User.Identity.IsAuthenticated=false;
HttpContext.Current.User.Identity.Name==""
解释:当用户登录时,服务器为确认客户端通过验证要通过cookie向客户端写验证(Authenticat)信息,在登录页面刚验证完成后服务器 还没有把cookie 回发到Client,所以会没有值,当服务器第二次Response的时候,就会从客户端读取Cookie,要想有此Cookie还要在 web.config文件中配置相应的参数
<system.web>
<authentication mode="Forms">
<forms domain="bokoAdmin" timeout="20" loginUrl="Login.aspx" path="/"></forms>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
<system.web>
程序验证:
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);
}
其中加粗体为主要语句,有此一句就可以实现HttpContext.Current.User.Identity.IsAuthenticated=true;
[声明]本网转载网络媒体稿件是为了传播更多的信息,此类稿件不代表本网观点,本网不承担此类稿件侵权行为的连带责任。故此,如果您发现本网站的内容侵犯了您的版权,请您的相关内容发至此邮箱【27535611@qq.com】,我们在确认后,会立即删除,保证您的版权。