When developing the Adam SharePoint connector we needed to use the Adam AD integration to be invoked within the SharePoint context. It appeared that the SharePoint modules somehow got in the way of the out-of-the-box AdamModule (Adam.Web.Core.AdamModule). Here's the workaround we used to overcome this:
We created the following HttpModule:
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Adam.Web.Core;
using Adam.Web.Core.Authentication;
using System.Web.UI;
using Adam.Core;
using Adam.Core.Server;
using System.Security.Principal;
using Adam.Web.Core.Helpers;
using Adam.Tools.Converters;
using Adam.Tools.LogHandler;
using Adam.Web.Core.Configuration;
///
/// Module to use AD authentication within a custom application.
///
public class AdamSharePointAuthenticationModule : IHttpModule
{
private const string ADAMSESSION = ".ADAMSESSION";
#region IHttpModule Members
public void Dispose()
{
// Nothing to dispose.
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
private void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication oApplication = sender as HttpApplication;
HttpRequest oRequest = oApplication.Request as HttpRequest;
HttpResponse oResponse = oApplication.Response as HttpResponse;
Application oApp;
oApp = new Application();
if (oRequest.Cookies[ADAMSESSION] != null)
{
Guid gSessionId = Guid.Empty;
if (GuidConverter.TryParse(oRequest.Cookies[ADAMSESSION].Value, out gSessionId))
{
oApp.LogOn(null, gSessionId);
}
}
//If we're unable to pickup a valid session try to (re)authenticate using AD
if (!oApp.IsLoggedOn)
{
if (oApp.LogOn(null, null, null, false, null, new ActiveDirectoryAuthenticationProvider()) == LogOnStatus.LoggedOn)
{
oResponse.Cookies[ADAMSESSION].Value = oApp.SessionId.ToString();
}
}
//If the module is unable to pickup a valid session or authenticate using AD -> throw exception
if (!oApp.IsLoggedOn)
{
throw new InvalidOperationException("Unable to logon to Adam");
}
AdamContext.Current.Applications.Add(oApp);
}
#endregion
}
|
What's important here is that this module stores the Adam application object in the AdamContext object. We needed to store the session id in a cookie; not only for reauthenticating at each page request, but also for each webpart. This module gets invoked for each page request and each webpart. Not doing this could have a huge performance impact!
The other thing that you need to be careful with is when this module gets invoked. It needs to be invoked AFTER the AdamModule and BEFORE the SharePoint session state module (System.Web.SessionState.SessionStateModule):
| XML |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<httpModules>
<clear />
<add name="SPRequest" type="Microsoft.SharePoint.ApplicationRuntime.SPRequestModule, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
<add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
<!-- <add name="Session" type="System.Web.SessionState.SessionStateModule"/> -->
<add name="PublishingHttpModule" type="Microsoft.SharePoint.Publishing.PublishingHttpModule, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<add name="AdamModule" type="Adam.Web.Core.AdamModule, Adam.Web.Core, Version=4.4.0.0, Culture=neutral, PublicKeyToken=63f11f167f68d05b" />
<add name="AdamSPModule" type="Adam.SharePoint.Web.Core.AdamSPModule, Adam.SharePoint.Web.Core, Version=1.1.0.0, Culture=neutral, PublicKeyToken=3266306e8df4a2d3" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" />
</httpModules>
|
That's it. You should now be able to get a hold of the standard application object in the way you used to in other web UI's.