How to Display a Logon Disclaimer Notice Banner in Sharepoint by Customizing the Global Asax and Deploy the Global Asax File Using the Sharepoint Wsp

This post details a SharePoint approach for showing a DoD-style session disclaimer banner by customizing `Global.asax` and deploying it through WSP features. It separates banner/session logic from activation/deployment logic and includes backup/restore handling for safe feature deactivation.

Coveros Staff

December 12, 2012

I was working on a SharePoint DoD project, due to security requriements(STIG) it needed to display a disclaimer notice banner when a user initiates a session with the SharePoint Site. This solution tells how to customize and deploy the SharePoint Global.asax that triggers the new session start event to display the disclaimer notice banner.

This solution was split into two SharePoint projects because of following reasons:

  • DLL must be installed in the web-application /bin directory to be accessible by the global.asax SharePoint page that triggers the new session event. (Project: DoDNotice)
  • DLL must be installed in the GAC to work properly as a web-application feature. (Project : DoDNoticeActivator)

1. DoDNotice Project: Customize the SharePoint Global.asax to display disclaimer notice banner on a new Session Start

  • Create a new SharePoint project “DoDNotice”. Right click on “DoDNotice” project and select properties.
  • Set the project “Assembly Deployment Package” property to “WebApplication“.
  • Add the SharePoint Mapped folder “{SharePointRoot}\Template\Layouts”.
  • Create a new aspx file name it “Banner.aspx” in “\Template\Layouts” folder. (This is the page that shows the logon banner message.)
  • Add the following code to “Banner.aspx” file.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Welcome</title>

<style type=”text/css”>

body {

background-color: #133950;

color: black;

font-family: Arial,Helvetica,Tahoma,sans-serif;

font-size: 0.9em;

margin: 0;

text-align: left;

}

.notice {

background-color: white;

margin: 2em;

padding: 1em;

}

.notice div { text-align: center; }

</style>

 

</head>

<body>

<div>

<div>

<script type=”text/javascript”>

/* <![CDATA[ */

var params = {};

(function () {

var e,

a = /\\+/g,

r = /([^&=]+)=?([^&]*)/g,

d = function (s) { return decodeURIComponent(s.replace(a, “”)); },

q = window.location.search.substring(1);

while (e = r.exec(q)) {

params[d(e[1])] = d(e[2]);

}

})();

var uri;

if (‘uri’ in params) {

uri = params[‘uri’];

} else {

uri = ‘/’;

}

var warning = \”<h2>DOD NOTICE AND CONSENT BANNER</h2><p>Your Message</p>”;

function setBannerCookie() {

window.location=uri;

}

document.write(warning);

document.write(‘<div><button type=\”button\” style=\”width:50px;\” onclick=\”return setBannerCookie();\”>OK</button></div>’);

/* ]]> */

</script>

<noscript>

<p>

This site requires Javascript and cookies for access.</p>

</noscript>

</div>

</div>

</body></html>

  • Add a file “Global.asax” in “\Template\Layouts” folder.
  • Add the following code in Global.asax.cs: This code redirects to “Banner.aspx” page when a new Session starts.

protected void Session_Start(object sender, EventArgs e) {

// Code that runs when a new session is started

string requestUrl = Request.Url.ToString();

string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + “/_layouts/Banner.aspx?uri=”;

if (!requestUrl.Contains(“uri”))

Response.Redirect(baseUrl + requestUrl);

else

Response.Redirect(requestUrl); }

  • Deploy the solution.
  • You should see a “Banner.aspx” and “Global.asax” files in HIVE under “SharePointRoot}\Template\Layouts” folder.

2. DoDNoticeActivator Project: Deploy the custom global.asax file using the Sharepoint WSP package and Feature activation

  • Create a new SharePoint project “DoDNoticeActivator”. Right click on “DoDNoticeActivator” and select properties.
  • Set the project “Assembly Deployment Package” property to “GlobalAssemblyCache”.
  • Right click the Feature folder and add a new Feature “ActivateDoDBanner”.
  • In “ActivateDoDBanner” feature select the Scope “WebApplication”. The feature xml file should look like this:
<?xml version="1.0" encoding="utf-8" ?>

<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”

Description=”This feature adds the custom global.asax to web application virtual directory and take the backup of the original global.asax file.”

Id=”602D09E4-4F0D-4058-951D-55059BA87943″

Scope=”WebApplication”

Hidden=”False”

Title=”Activate DoD Banner/Notice”

Version=”1.0.0.0″

ReceiverAssembly=”DoDNoticeActivator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=88ce7fc8bcece008″

ReceiverClass=”DoDNoticeActivator.ActivateDoDBanner”>

</Feature>

  • Feature declares a FeatureReciever that needs to be coded to copy the custom ‘Global.asax’ file to the root directory of site that servers incoming traffic from internet (Internet Zone).
  • Right click on “ActivateDoDBanner” feature and Add Feature Recevier. Here is the FeatureActivated and FeatureDeactivating code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

try

{

// loop through each IisSettings dictionary pair (zones [dafault, intranet, internet, etc.] configured for web application)

foreach (KeyValuePair<SPUrlZone, SPIisSettings> pair in webApp.IisSettings)

{

// Deploy the customize global.asax to hive and take the back up of sharepoint original global.asax.

string srcFileinfo = Microsoft.SharePoint.Utilities.SPUtility.GetGenericSetupPath(@”TEMPLATE\LAYOUTS\Global.asax”);

string destFileinfo = pair.Value.Path.FullName.ToString() + “\\global.asax”;

 

if (File.Exists(destFileinfo.ToString())

&& (!File.Exists(destFileinfo + “.original”)))

{

// Take the backup of original global.asax . This will happen first time

File.Copy(destFileinfo.ToString(), destFileinfo.ToString() + “.original”, true);

}

if (File.Exists(destFileinfo.ToString())

&& (!File.Exists(destFileinfo + “.hivebackup”)))

{

// Take the backup of original global asax.

File.Copy(destFileinfo.ToString(), destFileinfo.ToString() + “.hivebackup”, true);

// Copy the customize global.asax to hive.

File.Copy(srcFileinfo.ToString(), destFileinfo.ToString(), true);

}

}

}

catch (Exception ex)

{

throw new Exception(“Feature Activation error:” + ex.Message);

}

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

// loop through each IisSettings dictionary pair (zones [dafault, intranet, internet, etc.] configured for web application)

try

{

foreach (KeyValuePair<SPUrlZone, SPIisSettings> pair in webApp.IisSettings)

{

// Restore the Original sharepoint global.asax in hive and delete the back up copy of global.asax

string destFileinfo = pair.Value.Path.FullName.ToString() + “\\global.asax”;

//RemoveFromHive(properties, destFileinfo);

if (File.Exists(destFileinfo + “.hivebackup”))

{

// make the copy of current global asax.

File.Copy(destFileinfo, destFileinfo + “.custom”, true);

 

// copy hivebackup file back to the hive and make that the current global asax.

File.Copy(destFileinfo + “.hivebackup”, destFileinfo.ToString(), true);

File.Delete(destFileinfo + “.hivebackup”);

}

}

}

catch (Exception ex)

{

throw new Exception(“Feature Deactivation error:” + ex.Message);

}

}

 

  • Deploy the project “DoDNoticeActivator” to SharePoint server.
  • Go to SharePoint Central Admin > Manage Web Applications > Select Web Application > Manage Features > Activate the “ActivateDoDBanner” feature.
  • Go to web application virtual directory “C:\inetpub\wwwroot\wss\VirtualDirectories\{web application port}\
  • Verify that the custom global.asax, global.asax.original, global.asax.hivebackup files are there.

Enjoy the disclaimer notice banner on your Sharepoint site on a new Session Start.

Coveros Staff

Coveros Staff

This post represents the collective insights of the Coveros team. Our staff consists of software experts who bring deep experience in secure agile development, DevOps, testing, and software quality. Over the past 20 years, Coveros has trained more than 30,000 professionals and worked with half of the Fortune 100 companies on mission-critical software development challenges. We draw on this extensive experience to share practical insights, proven strategies, and real-world solutions that help organizations build better software faster and more securely.