如何重定向到登录页面时,会议已过期(ASP.NET 3.5 FormsAuthen) [英] How to redirect to LogIn page when Session is expired (ASP.NET 3.5 FormsAuthen)

查看:125
本文介绍了如何重定向到登录页面时,会议已过期(ASP.NET 3.5 FormsAuthen)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FormsAuthentication和ASP.Net SqlMembership提供商。
     我想提供一个功能重定向时的基本会话过期登录页面。
     我把codeS以下块在我的的BasePage OnInit的
     虽然我测试至今,它总是不断重定向到登录,即使我提供正确的用户ID和密码的页面。
     按理说,应该带我去的默认页。这是pretty紧迫和任何帮助将非常AP preciated。

问候,
Thurein

 如果(Context.Session = NULL&放大器;!&安培; Session.IsNewSession和放大器;&安培; this.Request.IsAuthenticated)
        {
            字符串cookieHeader = Request.Headers [曲奇];
            如果(cookieHeader = NULL&放大器;!&安培; cookieHeader.IndexOf(ASP.NET_SessionId)> = 0)
            {
                    HttpContext.Current.Session.Abandon();
                    FormsAuthentication.SignOut();
                    的Response.Redirect(FormsAuthentication.LoginUrl);            }
        }


解决方案

您不需要任何自定义code此功能,它是由框架的支持。只是配置在web.config:

 <身份验证模式=表格>
    <形式defaultUrl =〜/ Default.aspx的
        loginUrl =〜/的Login.aspx
        slidingExpiration =真
        超时=60/>
< /认证>

通过以​​上的结构中,用户将被始终当他们的会话到期重定向到Login.aspx页面。有60分钟的超时,滑动到期意味着超时每个用户进行Web应用程序的请求时间延长,所以如果他保持积极的会议将不会过期。像这样的配置给你另一个优势你试图做什么 - 一旦用户登录,他将被自动重定向回原来他请求的资源。而你总是可以覆盖和定制此行为。

I'm using FormsAuthentication and ASP.Net SqlMembership Provider. I would like to provide a functionality to redirect to LogIn page when the underlying Session is expired. I put the following block of codes in my BasePage OnInit. As far as I tested, it always keeps on redirecting to LogIn page even though I supplied correct UserID and Password. By rights, it should take me to default page. It's pretty urgent and any help would be much appreciated.

Regards, Thurein

if (Context.Session != null && Session.IsNewSession && this.Request.IsAuthenticated)
        {
            string cookieHeader = Request.Headers["Cookie"];
            if (cookieHeader != null && cookieHeader.IndexOf("ASP.NET_SessionId") >= 0)
            {


                    HttpContext.Current.Session.Abandon();                       
                    FormsAuthentication.SignOut();                        
                    Response.Redirect(FormsAuthentication.LoginUrl);                                           

            }
        }

解决方案

You don't need any custom code for this functionality, it's supported by the Framework. Just configure it in the web.config:

<authentication mode="Forms">
    <forms defaultUrl="~/Default.aspx"
        loginUrl="~/Login.aspx"
        slidingExpiration="true"
        timeout="60" />
</authentication>

With the configuration above, the user will be always redirected to the Login.aspx page when their session expires. There is a timeout of 60 minutes, and sliding expiration means that the timeout is extended each time the user makes a request to the web application, so if he stays active the session will not expire. A configuration like this gives you another advantage over what you tried to do - once the user logs in he will be automatically redirected back to the resource he originally requested. And you can always override and customize this behavior.

这篇关于如何重定向到登录页面时,会议已过期(ASP.NET 3.5 FormsAuthen)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆