SharePoint 2013-会话结束时重定向

本文关键字:重定向 结束 会话 2013- SharePoint | 更新日期: 2023-09-27 18:28:12

我创建了Global.asax文件,并将其放入我的web应用程序中,该应用程序可在SharePoint 2013上运行。在Global.asax.cs文件中,我继承了SPHttpApplication类并重写BeginRequest事件。它不起作用,Global.asax文件被SharePoint忽略,或者我错过了其他可添加的内容。目标是在会话过期时创建重定向。我做错了什么?这是我在代码背后的代码:

namespace PhoenixHR.Global  
{
    public class PhenixHRHttpApplication : SPHttpApplication
    {
        public override void Init()  
        {      
            try  
            {  
                base.Init();  
                this.BeginRequest += new EventHandler(Moj_BeginRequest);  
            }  
            catch (Exception ex)  
            {  
            }  
        }
        private void Moj_BeginRequest(object sender, EventArgs e)  
        {  
            try  
            {  
             if (HttpContext.Current.Session["TPL"] == null)   
                Response.Redirect("~/PhoenixHR/start.aspx");  
            }  
            catch (Exception ex)  
            {  
            }  
        }
    }
}

Global.asax

<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Application Language="C#" Inherits="PhoenixHR.Global.PhenixHRHttpApplication,PhoenixHR.Global,Version=1.0.0.0,Culture=neutral,PublicKeyToken=a0bd8fabe3543dc0" %>

SharePoint 2013-会话结束时重定向

由于您的代码中有一些异常,您能证明更改不起作用吗?还是每次请求都会引发异常而忽略它?

绑定到不同的事件会更好。HttpApplication类有相当多的事件可以绑定,并且BeginRequest在处理请求管道的早期(第一个)。这将无法访问会话状态。我建议使用AcquireRequestState或PostAcquireRequest State。

为了实现这一点,您需要创建一个HttpModule。

namespace PhoenixHR.Global
{
    public class SessionMissingRedirectHttpModule : IHttpModule
    {
        private const string LAYOUTS_SUBFOLDER = "/PhoenixHR.Global";
        private const string START_PAGE = "/start.aspx";
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            context.PostAcquireRequestState += context_PostAcquireRequestState;
        }
        void context_PostAcquireRequestState(object sender, EventArgs e)
        {
            // get required contexts
            SPHttpApplication application = (SPHttpApplication)sender;
            HttpContext context = application.Context;
            HttpSessionState session = context.Session;
            if (session == null)
                return;
            if (session["TPL"] != null)
                return;
            // get SharePoint context and current SPWeb.
            SPContext sharepointContext = SPContext.GetContext(context);
            SPWeb currentWeb = sharepointContext.Web;
            // build a url to the redirect page.
            string layoutsFolder =
                SPUtility.GetLayoutsFolder(currentWeb);
            string url = string.Format("{0}/{1}{2}{3}", currentWeb.Url, layoutsFolder, LAYOUTS_SUBFOLDER, START_PAGE);
            // prevent redirection loop
            if (context.Request.Url.AbsoluteUri.Equals(url, StringComparison.InvariantCultureIgnoreCase))
                return;
            SPUtility.Redirect(url, SPRedirectFlags.Trusted, context);
        }
    }
}

要注册HttpModule,您需要编辑它将在其中运行的web应用程序的web.config。

在/Configuration/system.webServer/modules路径中添加以下

<add name="SessionMissingRedirectHttpModule" type="PhoenixHR.Global.SessionMissingRedirectHttpModule, PhoenixHR.Global,Version=1.0.0.0,Culture=neutral,PublicKeyToken=a0bd8fabe3543dc0" />

对于所需应用程序页面的路径,有一些最佳猜测,因此您可能需要根据需要更改变量。