基于角色的Form验证登录一会儿后,要求重新登录.找不到原因.大家帮帮我啊...帮顶有分喔!!!!!!!

jerrycool 2006-11-03 04:02:44
代码
Login.aspx.cs:
如果用户名密码正确:
//设置登陆信息
string Role="Administrator";//角色:管理员
//建立role-based认证票据
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,Name,DateTime.Now,DateTime.Now.AddMinutes(120),true,Role);
//修改票据cookie,使其加密(本质是写入一个与票据cookie同名的新cookie)
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);
//cookie的有效时间
authCookie.Expires = DateTime.Now.AddMinutes(120);
Response.Cookies.Add(authCookie);
cookie.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookie);
string url = FormsAuthentication.GetRedirectUrl(Name,false);
if(url.ToLower().IndexOf("default.aspx") > 0)
{
url = "setup.aspx";
}
Response.Redirect(url);

web.config:
<authentication mode="Forms">
<forms name="Chris.Flower" loginUrl="Admin/Login.aspx" protection="All" timeout="30" path="/" />
</authentication>
<authorization>
<allow users="*" />
</authorization>
<location path="Admin">
<system.web>
<authorization>
<allow roles="Administrator" />
<deny users="*" />
</authorization>
</system.web>
</location>

Global.asax.cs:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if(HttpContext.Current.User != null)//如果当前的http信息中存在用户信息
{
if(HttpContext.Current.User.Identity.IsAuthenticated)//如果当前用户的身份已经通过了验证
{
if(HttpContext.Current.User.Identity is FormsIdentity)
{
//如果当前用户身份是FormsIdentity类即窗体验证类,此类有个属性能够访问当前用户的验证票
FormsIdentity fi = (FormsIdentity)HttpContext.Current.User.Identity;//创建个FormsIdentity类,用他来访问当前用户的验证票
//获得用户的验证票
FormsAuthenticationTicket ticket = fi.Ticket;
//从验证票中获得用户数据也就是角色数据
string userData = ticket.UserData;
//把用户数据用|分解成角色数组
string[] roles = userData.Split('|');
//重写当前用户信息,就是把角色信息也加入到用户信息中
HttpContext.Current.User = new GenericPrincipal(fi,roles);
}
}
}
}

PS:Login页面,那两个设置时间的地方一直没弄明白怎么回事.还有web.config文件那里也有一个.
问题就是登录一会儿后,要求重新登录.要求是:登录后,只要浏览器不关,则不会超时(如果做不到,可以定时刷新).关闭浏览器,就要求登录.
...全文
589 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
iclife 2006-11-07
  • 打赏
  • 举报
回复
mark!
jerrycool 2006-11-07
  • 打赏
  • 举报
回复
孟老大,webdiyer
帮我分析一下吧...这是为什么呢?

昨天我想了一下,如下:
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);
authCookie.Path = FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(authCookie);
把目光放到这里!!!以上设置的Cookies都是authTicket的吧
但authTicket是作为authCookie的值存放的,我们是不是设置错了对象呢?
所有的设置应该是authCookie.不是吗?
试一下再回帖.

另请其它高手也帮忙看下...实在郁闷之极
孟子E章 2006-11-06
  • 打赏
  • 举报
回复
判断是否是你机器的问题,换个机器测试看看啊,如果都使用cookie应该没有问题的。Session倒是经常出问题
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
孟子老大,还是不行啊
yagangshi 2006-11-06
  • 打赏
  • 举报
回复
up
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
顶上去
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
应该不是啊.全公司员工都是登录后一会儿就超时了
火雷 2006-11-06
  • 打赏
  • 举报
回复
前两天我的电脑得了老年痴呆,不管什么网站的登陆,它都记不住了,只要关了稍等片刻,它就忘记,看了看cookie和ie的相关设置,全没问题。气得我咬牙切齿,最后干掉,ghost。你不会也得这病吧?
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
string strRedirect=Request["ReturnUrl"];
我现在改为这样,还是不行呢.

如果为true的话,那不是建立永远Cookie吗?我只想要浏览器没关的时候Cookie还在,关掉后就要重新登录
孟子E章 2006-11-06
  • 打赏
  • 举报
回复
string url = FormsAuthentication.GetRedirectUrl(Name,true);
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
期待高手
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
最新记录结果:

第一次查看:
发出去的时间:2006-11-6 20:40:53
过期的时间:2006-11-6 21:10:53
刷新页面时间:2006-11-6 20:55:53

第二次查看:
发出去的时间:2006-11-6 20:55:53
过期的时间:2006-11-6 21:25:53
刷新页面时间:2006-11-6 20:56:01

第三次查看时,已经超时了,记录时间为:2006-11-6 21:12:34

同样超时是30分钟,我在发出去15分钟后再次请求页面,Cookie已经更新了发出去的时间和过期时间.我以为问题解决了...不过.NET好象在跟我开玩笑,21:12:34再刷新页面时,超时了.明明第二次查看时要到21:25:53秒才会过期超时啊....
以下是记录时间的代码:
FormsIdentity fi = (FormsIdentity)HttpContext.Current.User.Identity;
是否持久Cookie:"+fi.Ticket.IsPersistent
发出去的时间:"+fi.Ticket.IssueDate
过期的时间:"+fi.Ticket.Expiration
刷新页面时间:"+DateTime.Now.ToString()
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
oldmoon(浪者)
timeout=30去年有用吗?没试过...我想应该...

adandelion(水源是CSDN最黑的地方,但这个最黑是CSDN一手制造的!)
Forms验证用的是Session么?不是Session丢失,是Cookie!
adandelion 2006-11-06
  • 打赏
  • 举报
回复
Session丢失?
使用下列模式,然后在服务里启动asp.net状态服务.
<sessionState mode="StateServer" cookieless="false" timeout="30" stateConnectionString="tcpip=127.0.0.1:42424" />
dengkz1 2006-11-06
  • 打赏
  • 举报
回复
up
oldmoon 2006-11-06
  • 打赏
  • 举报
回复
<authentication mode="Forms">
<forms name="Chris.Flower" loginUrl="Admin/Login.aspx" protection="All" timeout="30" path="/" />

timeOut=30去掉,然后再按:
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,Name,DateTime.Now,DateTime.Now.AddMinutes(120),true,Role);
true - false
非永久性cookie不需要设过期时间
试试
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
就没人知道吗?
ygxx 2006-11-06
  • 打赏
  • 举报
回复
帮顶!!!
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
最新记录结果:
登录时间:16:57
17:04 ok!
17:07 ok!
17:11 ok!
17:16 ok!
17:20 ok!
17:24 ok!
17:30 到这里看到已经是Login.aspx的页面了
算了一下时间,应该是30分钟的时候,超时了...也就是说,Cookie在时间过半时,并没有更新?或者是哪里设置出了错,成了绝对过期时间?还是怎么了?
高手们???
jerrycool 2006-11-06
  • 打赏
  • 举报
回复
<frameset rows="*" cols="130,*" framespacing="0" frameborder="no" border="0">
<frame src="Left.htm" name="leftFrame" scrolling="No" noresize="noresize" id="leftFrame" />
<frame src="setup.aspx" name="mainFrame" id="mainFrame" />
</frameset>

我用框架,应该不是这个问题吧.mainFrame页面的setup.aspx是300自动刷新一次,leftFrame不变的.
加载更多回复(12)

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧