博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MOSS / Sharepoint 2007 Custom Error Page and Access denied Page
阅读量:4660 次
发布时间:2019-06-09

本文共 4767 字,大约阅读时间需要 15 分钟。

From:

 

In case if you are interested in creating your own custom error page and custom access denied page instead of the out of the box MOSS / Sharepoint error page (generally the requirements in Publishing internet site). You can use the following HTTPModule (this is just a sample, you can change it to suit your requirements):

 

Note to have following Web.config Entries:

Following entries in the <appsettings> section in the web.config file:

  1. Following entry is for a custom error page to be displayed for anonymous users, the key should be in the format: “CustomAnonCustErrPG-SiteCollectionURL” as shown below:

<add key="CustomAnonCustErrPG-http://xyz.com" value="/Pages/default.aspx" />

  1. Following entry is for a custom error page to be displayed for authenticated users, the key should be in the format: “CustomAuthCustErrPG-SiteCollectionURL” as shown below:

<add key="CustomAuthCustErrPG-http:// xyz.com " value="/Pages/default.aspx" />

  1. Following entry is for a custom error page to be displayed for access denied errors, the key should be in the format: “CustomCustAcsDndPG -SiteCollectionURL” as shown below:

<add key="CustomCustAcsDndPG-http://xyz.com" value="//CustomErrPg.aspx " />

 

Create an entry on all the WFEs web-application specific web.config in the HTTPModules section, as follows just below the <HTTPModule> tag This should be the first entry in the HTTPModules section:

<add name="XYZExceptionHandler" type="XYZ.Sharepoint.HTTPModule.CustomErrorPages.CustomErrors, XYZ.Sharepoint.HTTPModule.CustomErrorPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=PUBLICKEYGUID" />

 

NOTE: Add your own Public key for PublicKeyToken per your snk used, replace the PUBLICKEYGUID with your own snk value

 

///

// This is a HTTPModule used for redirecting the user to Home or Home Anon site depending on whether

// s/he is authenticated or anonymous.

//

// The module checks if the url is accessdenied.aspx and redirects the user to proper url.

 

using System;

using System.Web;

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

using System.Configuration;

using Microsoft.SharePoint;

 

namespace XYZ.Sharepoint.HTTPModule.CustomErrorPages

{

    public class CustomErrors : IHttpModule

    {

 

        #region -- Private fields --

 

        private static AppSettingsReader m_Reader = new AppSettingsReader();

        private static string m_SiteCollAnonCustomErrPG = "CustomAnonCustErrPG";

        private static string m_SiteCollAuthCustomErrPG = "CustomAuthCustErrPG";

        private static string m_SiteCollCustAcsDndPG = "CustomCustAcsDndPG";

       

        #endregion

 

 

        public void Init(HttpApplication context)

        {

            //Attaching event handlers for access denied and error respectively

            //context.BeginRequest += new EventHandler(context_AcessDenied);

            context.EndRequest += new EventHandler(context_AcessDenied);

            context.Error += new EventHandler(context_Error);

 

        }

 

        void context_AcessDenied(object sender, EventArgs e)

        {

            try

            {

                HttpApplication httpApp = sender as HttpApplication;

                HttpContext context = httpApp.Context;

 

                string httpUrl = context.Request.Url.ToString();

                string strCustomAcssDndURL = string.Empty;

                string strSiteURL = string.Empty;

 

                if (httpUrl.ToLower().Contains("/_layouts/accessdenied.aspx"))

                {

                    SPSecurity.RunWithElevatedPrivileges(delegate()

                    {

                        using (SPSite site = new SPSite(httpUrl))

                        {

                            strSiteURL = site.Url;

                        }

                    });

                    HttpContext.Current.Server.ClearError();

                    HttpContext.Current.Response.Clear();

                    strCustomAcssDndURL = getAppSettingsValue(m_SiteCollCustAcsDndPG + "-" + strSiteURL);

                    HttpContext.Current.Response.Redirect(strCustomAcssDndURL, false);

                }

            }

            catch (Exception ex)

            {

                //Do your exception handling

            }

          }

 

 

        void context_Error(object sender, EventArgs e)

        {

            try

            {

                string strURL = string.Empty;

                string strSiteURL = SPContext.Current.Site.Url;

                Exception[] unhandledExceptions = HttpContext.Current.AllErrors;

                //logging all the exceptions thru the Exception policy file

                foreach (Exception ex in unhandledExceptions)

                {

                //Do your exception handling

                }

 

                HttpContext.Current.Server.ClearError();

                HttpContext.Current.Response.Clear();

 

                if (HttpContext.Current.User.Identity.IsAuthenticated)

                {

                    strURL = getAppSettingsValue(m_SiteCollAuthCustomErrPG + "-" + strSiteURL);

                    HttpContext.Current.Response.Redirect(strURL, false);

                }

                else

                {

                    strURL = getAppSettingsValue(m_SiteCollAnonCustomErrPG + "-" + strSiteURL);

                    HttpContext.Current.Response.Redirect(strURL, false);

                }

            }

            catch (Exception ex)

            {

                //Do your exception handling

            }

        }

        

        public void Dispose()

        {

        }

 

        //Method used to read Appsettings value from web.config for the webapplication

        private static string getAppSettingsValue(string sKey)

        {

            string sValue = null;

 

            try

            {

                sValue = m_Reader.GetValue(sKey, typeof(string)) as string;

            }

            catch (Exception ex)

            {

                                //Do your exception handling

            }

 

            return sValue;

        }

    }

}

转载于:https://www.cnblogs.com/Jeffer/archive/2009/11/26/1611610.html

你可能感兴趣的文章
jQuery类级别插件--返回顶部,底部,去到任何部位
查看>>
胡思乱想
查看>>
让Oracle 大小写敏感 表名 字段名 对像名
查看>>
1021. Deepest Root (25) -并查集判树 -BFS求深度
查看>>
男人二十岁后应该学会的习惯
查看>>
PHP学习 Session 学习
查看>>
ember.js:使用笔记5 使用view
查看>>
Java线程停止interrupt()方法
查看>>
使用SVN进行源码管理
查看>>
[转]asp:ScriptManager
查看>>
WC 2018/CTSC 2018/APIO 2018 游记
查看>>
CodeForces - 997C Sky Full of Stars
查看>>
多个线程访问url
查看>>
yum搭建 Linux+Nginx+Mysql+Tomcat(负载均衡,动静分离)
查看>>
HTML错误码
查看>>
泛型集合的五中遍历方式
查看>>
cocos2dx游戏开发——微信打飞机学习笔记(九)——BulletLayer的搭建
查看>>
wpf log4net使用
查看>>
python之路,正则表达式
查看>>
eclipse中java项目的创建
查看>>