Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more


Aug 22, 2012
  4580
(6 votes)

Active Directory Membership Provider for EPiCommerce

A number of clients have asked, over the years, for a way to use ActiveDirectory as their membership provider in Commerce.  This can be done fairly easily.  The issue in the past has been that the user object in Commerce uses a GUID whereas a MembershipUser in Active Directory uses an SSID.  The goal of the below code is to intercept the call to 'GetUser' and to return a slightly modified MembershipUser that has all the information from the Commerce user object, but the corresponding SSID from its AD equivalent.  This allows us to plug into the AD membership provider without modifying any other code.

The first step in inserting the custom AD provider is to add a new CSPROJ file.  You may name it whatever you'd like, but in keeping with EPiServer naming conventions, it is recommended you name it "EPiServer.Commerce.ADMembershipProvider". 

Next, add the following block to the membership section of your web.config (please note that the value of attributeMapUsername must be 'sAMAccountName'.  That is not a "call it what you'd like" value)

   1: <membership defaultProvider="ActiveDirectoryMembershipProvider" userIsOnlineTimeWindow="10">
   2:   <providers>
   3:     <clear />
   4:     <add name="ActiveDirectoryMembershipProvider" 
   5:          type="EPiServer.Commerce.ADMembershipProvider.CustomADProvider, EPiServer.Commerce.ADMembershipProvider" 
   6:          connectionStringName="ActiveDirectoryProviderConnection" 
   7:          connectionUsername="domain\username" 
   8:          connectionPassword="password" 
   9:          enableSearchMethods="true" 
  10:          attributeMapUsername="sAMAccountName" />
Next, add a new LDAP connection to your domain in your connectionStrings.config:
   1: <add name="ActiveDirectoryProviderConnection" connectionString="LDAP://yourDoman"/>
   2:  

Finally, create a new class under your new project.  This may also be called whatever you'd like, but naming conventions recommend "CustomADProvider".  The important thing about this class is that
it inherits System.Web.Security.ActiveDirectoryMembershipProvider, the methods of which we will override with the below code.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using System.Web.Security;
   6: using System.DirectoryServices;
   7: using System.Configuration;
   8: using System.Web;
   9: using System.Security.Principal;
  10: using System.Globalization;
  11: using System.Configuration.Provider;
  12: using System.Security.Permissions;
  13:  
  14: namespace EPiServer.Commerce.ADMembershipProvider
  15: {
  16:     public class CustomADProvider : ActiveDirectoryMembershipProvider
  17:     {
  18:         private string connUser = string.Empty;
  19:         private string connPassWord = string.Empty;
  20:         private string connectionString = string.Empty;
  21:         private string ldapConnection = string.Empty;
  22:  
  23:         public override MembershipUser GetUser(string username, bool userIsOnline)
  24:         {
  25:             MembershipUser user = base.GetUser(username, userIsOnline);
  26:  
  27:             MembershipUser returnUser = ReturnModifiedUser(user);
  28:  
  29:             return returnUser;
  30:         }
  31:  
  32:         public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
  33:         {
  34:             if (providerUserKey == null)
  35:             {
  36:                 throw new ArgumentNullException("providerUserKey");
  37:             }
  38:  
  39:             try
  40:             {
  41:                 Guid guid = (Guid)providerUserKey;
  42:                 StringBuilder guidHexValueStr = new StringBuilder();
  43:  
  44:                 byte[] guidArray = guid.ToByteArray();
  45:                 int binaryLength = guidArray.Count();
  46:                 for (int i = 0; i < binaryLength; i++)
  47:                 {
  48:                     guidHexValueStr.Append("\\");
  49:                     guidHexValueStr.Append(guidArray[i].ToString("x2", NumberFormatInfo.InvariantInfo));
  50:                 }
  51:  
  52:                 String filter = " (objectGuid=" + guidHexValueStr.ToString() + ")";
  53:  
  54:                 try
  55:                 {
  56:                     DirectoryEntry de = new DirectoryEntry(ldapConnection, connUser, connPassWord, AuthenticationTypes.Secure);
  57:                     DirectorySearcher deSearch = new DirectorySearcher();
  58:                     deSearch.SearchRoot = de;
  59:                     deSearch.Filter = filter;
  60:                     SearchResult result = deSearch.FindOne();
  61:  
  62:                     if (result != null)
  63:                     {
  64:                         DirectoryEntry entry = result.GetDirectoryEntry();
  65:                         MembershipUser user =  base.GetUser(entry.Name, userIsOnline);
  66:                         MembershipUser newUser = new MembershipUser(this.Name, user.UserName, entry.Guid, user.Email, user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreationDate, user.LastLoginDate, user.LastActivityDate, user.LastPasswordChangedDate, user.LastLockoutDate);
  67:                         return newUser;
  68:                     }
  69:  
  70:                     return null;
  71:                 }
  72:                 catch
  73:                 {
  74:                     throw;
  75:                 }
  76:  
  77:             }
  78:             catch
  79:             {
  80:                 throw;
  81:             }
  82:         }
  83:  
  84:         [DirectoryServicesPermission(SecurityAction.Demand, Unrestricted = true), DirectoryServicesPermission(SecurityAction.Assert, Unrestricted = true), DirectoryServicesPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
  85:         public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
  86:         {
  87:             connUser = config["connectionUsername"];
  88:             connPassWord = config["connectionPassword"];
  89:             connectionString = config["connectionStringName"];
  90:             ldapConnection = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
  91:  
  92:             base.Initialize(name, config);
  93:         }
  94:  
  95:         private MembershipUser ReturnModifiedUser(MembershipUser user)
  96:         {
  97:             DirectoryEntry de = new DirectoryEntry(ldapConnection, connUser, connPassWord, AuthenticationTypes.Secure);
  98:             DirectorySearcher deSearch = new DirectorySearcher();
  99:             deSearch.SearchRoot = de;
 100:             deSearch.Filter = String.Format("sAMAccountName={0}", user.UserName);
 101:             SearchResult result = deSearch.FindOne();
 102:  
 103:             if (result != null)
 104:             {
 105:                 DirectoryEntry entry = result.GetDirectoryEntry();
 106:                 MembershipUser newUser = new MembershipUser(this.Name, user.UserName, entry.Guid, user.Email, user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreationDate, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
 107:                 return newUser;
 108:             }
 109:  
 110:             return null;
 111:         }
 112:  
 113:     }
 114: }
Aug 22, 2012

Comments

valdis
valdis Aug 22, 2012 01:30 PM

Just remember if you are using membership provider AS-IS your editor - you may have encounter some search problems against AD. More details here - http://bergdaniel.se/using-the-active-directory-membership-provider-with-episerver.

Nikolay Moskalev
Nikolay Moskalev Sep 6, 2012 11:58 AM

Dan, great job! Thank you very much! It's works! I can turn on AD authentication for EPiCommerce with your help!

Please login to comment.
Latest blogs
Optimizely CMS Developer Tools for macOS

Running Optimizely CMS on macOS presents unique challenges, as the platform was traditionally primarily designed for Windows environments. However,...

Tomek Juranek | Mar 15, 2025

Removing a Segment from the URL in Optimizely CMS 12 using Partial Routing

Problem Statement In Optimizely CMS 12, dynamically generated pages inherit URL segments from their container pages. However, in certain cases, som...

Adnan Zameer | Mar 14, 2025 |

Optimizely Configured Commerce and Spire CMS - Figuring out Handlers

I recently entered the world of Optimizely Configured Commerce and Spire CMS. Intriguing, interesting and challenging at the same time, especially...

Ritu Madan | Mar 12, 2025

Another console app for calling the Optimizely CMS REST API

Introducing a Spectre.Console.Cli app for exploring an Optimizely SaaS CMS instance and to source code control definitions.

Johan Kronberg | Mar 11, 2025 |

Extending UrlResolver to Generate Lowercase Links in Optimizely CMS 12

When working with Optimizely CMS 12, URL consistency is crucial for SEO and usability. By default, Optimizely does not enforce lowercase URLs, whic...

Santiago Morla | Mar 7, 2025 |

Optimizing Experiences with Optimizely: Custom Audience Criteria for Mobile Visitors

In today’s mobile-first world, delivering personalized experiences to visitors using mobile devices is crucial for maximizing engagement and...

Nenad Nicevski | Mar 5, 2025 |