Microsoft / SharePoint / MOSS Single Sign-On Service not supported in non AD Environment?

11 03 2009

After attempting for an hour or so to get the SSO service running on a MOSS system in a local machine (non Active Directory) environment, I discovered a couple of posts and some documentation from Microsoft which indicated that for the SSO service to be used with SharePoint/MOSS, the system must be integrated into an Active Directory system.

The problems come when you try to configure the SSO service in Central Admin, on the screen where you enter the Administration and Management credentials for the SSO service, when clicking OK here, you get presented with the Unknown Error Occurred or Access Denied error messages.





Update: Configure Sharepoint (MOSS) Single Sign-On

7 01 2009

As an update to my post about Configuring the Sharepoint MOSS Single Sign-On Service, I’ve discovered that the SSOADMIN user (the user account which the SSO service is run under) requires that the user account is granted the “Logon as Service” permission directly, rather than it being inherited by Group memberships or some other means.





Configure Sharepoint/MOSS Single Sign-On (SSO) Links

18 11 2008




Sharepoint Single Sign-On, Impersonation and the Double-Hop Problem

31 10 2008

How do you overcome the identity double hop problem?

Windows credentials can only make one “hop” between machines on a network. The first hop is from the user’s browser to the web server; from here, to get to another machine on your network, a second hop is required.

There are two ways to work around this problem: 1) establish a delegation relationship between the web server and the other network machine, and configure the AD domain to allow Kerberos Protocol Transition, or 2) use the Win32 LogonUser API to switch to the user’s identity on the web server before making that single hop out to the other network machine.

Sharepoint Single Sign-On uses the second approach. The first approach is complex and probably akin to using a sledge hammer to crack a nut.

The great thing about the Sharepoint SSO service is that when creating Enterprise Application Definition’s, you can decide what credential fields are stored, so you can store, obviously, User names and Passwords, DB connection strings, Domain names and, well, other stuff you can put in a string.

So, as an example, a web part needs to collect information from a network machine to display in it’s UI to a user. You have two choices here, either the resource access needs to be done under the security context of the user (impersonation model), or, the resource access can be done under the security context of some ad-hoc user account (the trusted sub-system model).

In summary the webpart will retrieve security credentials from SSO, create an impersonation security context with those credentials using the LogonUser API, perform the resource access and then undo the impersonation.

We can overcome the double-hop problem using Sharepoint SSO while fulfilling both security models;

1. Trusted Sub-system Model

Create an SSO Enterprise Application Definition of type Group – all users will access network resources using the same credentials:

MbosDoDefSSO Display Name:   Mbos ESB Domain Access
  Type: Group
  Fields:  
 
Username (Unmasked) = esbprocess
Password (Masked) = *****
Domain (Unmasked) = MIT

2. Impersonation Model

Create an SSO Enterprise Application Definition of type Individual – all users will access network resources using their own credentials:

MbosLoDefSSO Display Name:   Mbos ESB LogData Access
  Type: Individual
  Fields:  
 
Username (Unmasked) = hardingp
Password (Masked) = *****
Domain (Unmasked) = MIT

First you’ll need to configure Sharepoint SSO, try google or this post here.

So assuming that you’ve configured SSO and set up these EAD’s, the next requirement is some code to do the impersonation which you can write, find on google or copy this one here.

Finally, you’ll want some code to get credentials from SSO, which I’ve reproduced below.

One thing to note, is that if you create an EAD of type Individual (Windows Authentication), when you call ISsoProvider.GetCredentials, a SingleSignonException exception will be generated if SSO doesn’t have credentials stored for the calling user, for the EAD.

In this case, you can make a call to ISsoProvider.GetCredentialManagementUrl to get the credential management URL to allow the user to enter their SSO credentials (for this EAD).

Putting these pieces together, accessing network resources either on behalf of the calling user, or as an ad-hoc user, can be accomplished as shown below;

    // get sso creds
    var ssoApp = SharepointSSO.GetEnterpriseApplication(C_SSO_EadId);
    
    using (new Network.Impersonator(ssoApp.Fields["Username"], ssoApp.Fields["Domain"], ssoApp.Fields["Password"],
               Network.LogonType.LOGON32_LOGON_NEW_CREDENTIALS,
               Network.LogonProvider.LOGON32_PROVIDER_WINNT50))
    {
          // perform your network resource access here

    }

Sharepoint Single Sign-On accessor code.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Portal.SingleSignon;

namespace Tools.Sharepoint.SSO
{
 class SSOApplication
 {
  public IDictionary<string, string> Infomation
  { get; set; }

  public IDictionary<string, string> Fields
  { get; set; }
 }

 class SharepointSSO
 {
  private static string ConvertSecureStringToString(System.Security.SecureString pValue)
  {
   IntPtr lValuePointer = IntPtr.Zero;
   string lValueAsString;
   try
   {
    lValuePointer = Marshal.SecureStringToBSTR(pValue);
    lValueAsString = Marshal.PtrToStringBSTR(lValuePointer);
   }
   catch (Exception ex)
   {
    lValueAsString = ex.Message;
   }
   finally
   {
    if (lValuePointer != IntPtr.Zero)
     Marshal.ZeroFreeBSTR(lValuePointer);
   }
   return lValueAsString;
  }

  public static SSOApplication GetEnterpriseApplication(string eadID)
  {
   if (string.IsNullOrEmpty(eadID)) throw new ArgumentException("an EAD ID is required", "eadID");

   var ssoProvider = SsoProviderFactory.GetSsoProvider();
   var ssoCreds = ssoProvider.GetCredentials(eadID);
   var ssoApp = ssoProvider.GetApplicationInfo(eadID);
   var ssoFields = ssoProvider.GetApplicationFields(eadID);

   var creds = new SSOApplication
                {
                 Infomation = new Dictionary<string, string>(),
                 Fields = new Dictionary<string, string>()
                };
   creds.Infomation["ID"] = ssoApp.ApplicationName;
   creds.Infomation["Display Name"] = ssoApp.ApplicationFriendlyName;
   creds.Infomation["Contact Name"] = ssoApp.ContactName;
   creds.Infomation["Type"] = ssoApp.Type.ToString();

   for (int idx = 0; idx < ssoFields.Length; idx++)
   {
    string ssoEvidence = ConvertSecureStringToString(ssoCreds.Evidence[idx]);
    creds.Fields[ssoFields[idx].Field] = ssoEvidence;
   }

   return creds;
  }
 }
}




Exercising the Sharepoint (MOSS) 2007 Single Sign-On Service SDK

30 10 2008

In an effort to understand how you might exploit the SSO for your own custom development in Sharepoint (MOSS) I wrote a Web Part to enumerate SSO Applications and Credentials, as shown below.

The code for the web part is quite simple, as is the SSO SDK itself (at least as an SSO consumer).

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Portal.SingleSignon;

namespace Sharepoint.WebParts
{
 [Guid("45c8266e-6a1b-4223-97fa-5cc3e65c5629")]
 public class SSOInfoViewWebPart : System.Web.UI.WebControls.WebParts.WebPart
 {
  private static string ConvertSecureStringToString(System.Security.SecureString pValue)
  {
   IntPtr lValuePointer = IntPtr.Zero;
   string lValueAsString;
   try
   {
    lValuePointer = Marshal.SecureStringToBSTR(pValue);
    lValueAsString = Marshal.PtrToStringBSTR(lValuePointer);
   }
   catch (Exception ex)
   {
    lValueAsString = ex.Message;
   }
   finally
   {
    if (lValuePointer != IntPtr.Zero)
     Marshal.ZeroFreeBSTR(lValuePointer);
   }
   return lValueAsString;
  }

  protected override void CreateChildControls()
  {
   base.CreateChildControls();

   ISsoProvider ssoProvider = SsoProviderFactory.GetSsoProvider();
   var listOfEAD = ssoProvider.GetApplicationDefinitions();

   var sb = new StringBuilder();

   var ssoPI = ssoProvider.GetSsoProviderInfo();

   sb.Append("
<tr>
<td><b>SSO Provider:</td>
");
   sb.Append("
<td style=\"padding-left:10px;\">Vendor:</td>
");
   sb.AppendFormat("
<td>{0}</td>
</tr>
", ssoPI.Vendor);
   sb.Append("
<tr>
<td>&nbsp;</td>
");
   sb.Append("
<td style=\"padding-left:10px;\">Version:</td>
");
   sb.AppendFormat("
<td>{0}</td>
</tr>
", ssoPI.Version);
   sb.Append("
<tr>
<td>&nbsp;</td>
");
   sb.Append("
<td align=\"top\" style=\"padding-left:10px;\">Assembly:</td>
");
   sb.AppendFormat("
<td>{0}</td>
</tr>
", ssoPI.AssemblyName);

   var wi = WindowsIdentity.GetCurrent(true);
   if (wi != null)
   {
    var wic = WindowsIdentity.Impersonate(IntPtr.Zero);
    sb.Append("
<tr>
<td><b>Process Identity:</td>
");
    sb.AppendFormat("
<td style=\"padding-left:10px;\">{0}</td>
<td style=\"padding-left:10px;\">({1}, {2}{3}{4}{5}{6})</td>
</tr>
",
                    WindowsIdentity.GetCurrent().Name,
                    WindowsIdentity.GetCurrent().AuthenticationType,
                    WindowsIdentity.GetCurrent().ImpersonationLevel,
                    WindowsIdentity.GetCurrent().IsAnonymous ? ", Anonymous" : "",
                    WindowsIdentity.GetCurrent().IsAuthenticated ? ", Authenticated" : "",
                    WindowsIdentity.GetCurrent().IsGuest ? ", Guest" : "",
                    WindowsIdentity.GetCurrent().IsSystem ? ", System" : "");
    wic.Undo();
   }

   sb.AppendFormat("
<tr>
<td><b>{0} Identity:</td>
", wi != null ? "Thread" : "Process");
   sb.AppendFormat("
<td style=\"padding-left:10px;\">{0}</td>
<td style=\"padding-left:10px;\">({1}, {2}{3}{4}{5}{6})</td>
</tr>
",
                   WindowsIdentity.GetCurrent().Name,
                   WindowsIdentity.GetCurrent().AuthenticationType,
         WindowsIdentity.GetCurrent().ImpersonationLevel,
                   WindowsIdentity.GetCurrent().IsAnonymous ? ", Anonymous" : "",
                   WindowsIdentity.GetCurrent().IsAuthenticated ? ", Authenticated" : "",
                   WindowsIdentity.GetCurrent().IsGuest ? ", Guest" : "",
                   WindowsIdentity.GetCurrent().IsSystem ? ", System" : "");

   sb.Append("
<tr>
<td><b>ASP.NET Identity:</td>
");
   sb.AppendFormat("
<td style=\"padding-left:10px;\">{0}</td>
<td style=\"padding-left:10px;\">({1}{2})</td>
</tr>
",
         Context.User.Identity.Name,
         Context.User.Identity.AuthenticationType,
         Context.User.Identity.IsAuthenticated ? ", Authenticated" : "");

   sb.Append("
<tr>
<td><b>Sharepoint Identity:</td>
");
   sb.AppendFormat("
<td style=\"padding-left:10px;\">{0}</td>
<td style=\"padding-left:10px;\">(ID:{1}, {2}{3}{4}{5})</td>
</tr>
",
         SPContext.Current.Web.CurrentUser.Name,
         SPContext.Current.Web.CurrentUser.ID,
         SPContext.Current.Web.CurrentUser.LoginName,
         SPContext.Current.Web.CurrentUser.IsSiteAdmin ? ", SiteAdmin" : "",
         SPContext.Current.Web.CurrentUser.IsDomainGroup ? ", DomainGroup" : "",
         SPContext.Current.Web.CurrentUser.IsSiteAuditor ? ", SiteAuditor" : "");

   sb.Append("
<tr>
<td><b>SSO User:</b></td>
<td colspan=\"2\" style=\"padding-left:10px;\">");
   try
   { sb.AppendFormat("{0}", ssoProvider.GetCurrentUser()); }
   catch (Exception ex)
   { sb.AppendFormat("<i>n/a ({0})</i>", ex.Message); }
   sb.Append("</td>
</tr>
");

   sb.AppendFormat(
    "
<tr>
<td style=\"padding-bottom:5px;padding-top:10px\" colspan=\"3\"><b>SSO Enterprise Application Definitions</b></td>
</tr>
");
   foreach (var ead in listOfEAD)
   {
    string credManUrl = "#";
    if (ead.Type == Application.ApplicationType.Individual || ead.Type == Application.ApplicationType.IndividualWindows)
     credManUrl = ssoProvider.GetCredentialManagementURL(ead.ApplicationName).ToString();

    sb.Append("
<tr>");
    sb.AppendFormat("
<td style=\"border-top:solid 1px #B0BDC2;\"><b><a href=\"{1}\">{0}</a></b></td>
",
                    ead.ApplicationName, credManUrl);
    sb.Append("
<td style=\"padding-left:10px;border-top:solid 1px #B0BDC2;\">Display&nbsp;Name:&nbsp;&nbsp;</td>
");
    sb.AppendFormat("
<td style=\"border-top:solid 1px #B0BDC2;\">{0}</td>
", ead.ApplicationFriendlyName);
    sb.Append("</tr>
");

    sb.Append("
<tr>
<td>&nbsp;</td>
");
    sb.AppendFormat("
<td style=\"padding-left:10px;\">Type:</td>
<td>{0}</td>
", ead.Type);
    sb.Append("</tr>
");

    sb.Append("
<tr>
<td>&nbsp;</td>
");
    sb.Append("
<td style=\"padding-left:10px;\">Fields:</td>
<td>&nbsp;</td>
");
    sb.Append("</tr>
");

    sb.Append("
<tr>
<td>&nbsp;</td>
");
    sb.Append("
<td colspan=\"2\" style=\"padding-left:10px;padding-bottom:5px;\">");
    sb.Append("
<div style=\"padding-left:20px\">");

    var listOfEadFields = ssoProvider.GetApplicationFields(ead.ApplicationName);
    var listOfCreds = ssoProvider.GetCredentials(ead.ApplicationName);

    //    sb.AppendFormat("\"UserName\" = {0}
", ConvertSecureStringToString(listOfCreds.UserName));
    //    sb.AppendFormat("\"Password\" = {0}
", ConvertSecureStringToString(listOfCreds.Password));

    for (int idx = 0; idx < listOfEadFields.Length; idx++)
    {
     var eadField = listOfEadFields[idx];
     string ssoEvidence = ConvertSecureStringToString(listOfCreds.Evidence[idx]);
     sb.AppendFormat("{0} ({1}) = {2}
", eadField.Field, eadField.Mask ? "Masked" : "Unmasked", ssoEvidence);
    }

    sb.Append("</div>
");
    sb.Append("</td>
</tr>
");
   }

   Controls.Add(new LiteralControl("
<table cellspacing=\"0\" cellpadding=\"0\">"));
   Controls.Add(new LiteralControl(sb.ToString()));
   Controls.Add(new LiteralControl("</table>
"));
  }
 }
}