首页 新闻 搜索 专区 学院

.net中身份验证控件问题

1
悬赏园豆:10 [已关闭问题]
<P>我用FormsAuthentication.SetAuthCookie(user,false);创建一个身份验证票.然后用HttpContext.Current.User.Identity.IsAuthenticated 去检测是否通过验证.测试结果HttpContext.Current.User.Identity.IsAuthenticated&nbsp; 有时为true, 有时为flase</P> <P>但是</P> <P>用下面的方法创建时</P> <P>FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, user,DateTime.Now, DateTime.Now.AddMinutes(30), false, null, "/");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string HashTicket = FormsAuthentication.Encrypt(Ticket);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Context.Response.Cookies.Add(UserCookie);</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FormsIdentity identity = new FormsIdentity(Ticket);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GenericPrincipal user = new GenericPrincipal(identity, new string[] {});</P> <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpContext.Current.User = user;</P> <P mce_keep="true">HttpContext.Current.User.Identity.IsAuthenticated一直为true.</P> <P mce_keep="true">web.config中的配置为</P> <P mce_keep="true">&lt;authentication mode="Forms"&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;forms loginUrl="login.aspx" name="AuthCookie" timeout="60" path="/" defaultUrl="Default.aspx" cookieless="AutoDetect"&nbsp; &gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/forms&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/authentication&gt;</P> <P mce_keep="true">&nbsp;</P> <P mce_keep="true">分析一下这是什么原因? 为什么用FormsAuthentication.SetAuthCookie(user,false) 后,HttpContext.Current.User.Identity.IsAuthenticated还会为false呢?</P> <P mce_keep="true">&nbsp;</P>
engine的主页 engine | 初学一级 | 园豆:160
提问于:2008-04-15 14:22
< >
分享
其他回答(2)
0
将你要保存的数据放这个里面 System.Web.Security.FormsAuthentication.RedirectFromLoginPage(this.txtuname.Text.Trim(), true); 然后全程序都可以用 HttpContext.Current.User.Identity.Name 获取
yeyang | 园豆:418 (菜鸟二级) | 2008-04-15 16:48
0
看看这篇文章 http://www.cnblogs.com/yao/archive/2006/06/24/434783.html
wingoo | 园豆:1513 (小虾三级) | 2008-04-15 18:04
0
webconfig中加上这个试试 <authorization> <deny users="?"/> </authorization>
PENGHAO-X | 园豆:20 (初学一级) | 2008-04-16 13:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册