So, I got tired of the SDK documentation of this pretty nice system and did stuff by myself as usual. What I was trying to accomplish is to have one method that allows me to standardize the Customer Contact and MemberShip Account creation process no matter if you will interface the method from the web or through an integration or console. Lets call it flexible account creation method that works form Console, Web services and Web pages. The result below, have fun :)
Done in version EpiServer Commerce version 1 R2 SP1 (1.1.1) This article also applies to Mediachase Commerce 5.2
/// <summary> /// Create Contact and Membership User /// </summary> /// <param name="firstName"></param> /// <param name="lastName"></param> /// <param name="email">Also used as username</param> /// <param name="password"></param> private static void CreateUser(string firstName, string lastName, string email, string password) { // Ensure we have a datacontext, nice to have if we are outside the web envionment if (DataContext.Current == null) { string connectionstring = ConfigurationManager.ConnectionStrings["EcfSqlConnection"].ConnectionString; Mediachase.BusinessFoundation.Data.DataContext.Current = new Mediachase.BusinessFoundation.Data.DataContext(connectionstring); } string userName = email; string fullName = string.Concat(firstName, " ", lastName); // Try to get the user MembershipUser user = Membership.GetUser(userName); // If the used didn't exist lets create it if (user == null) user = Membership.CreateUser(userName, password, email); // Ensure user is approved user.IsApproved = true; Membership.UpdateUser(user); // Add user to default roles, if they exist try { if (RoleExists(AppRoles.EveryoneRole)) SecurityContext.Current.AssignUserToGlobalRole(user, AppRoles.EveryoneRole); if (RoleExists(AppRoles.RegisteredRole)) SecurityContext.Current.AssignUserToGlobalRole(user, AppRoles.RegisteredRole); } catch { } CustomerContact contact = CustomerContact.CreateInstance(user); contact.FullName = fullName; contact.FirstName = firstName; // Overwrites the default comming from CreateInstance contact.LastName = lastName; contact.RegistrationSource = "ByCode"; // Could be IP or anything contact.CustomerGroup = Constants.ProductCatalogConstants.DefaultCustomerGroup; // String value "Customers" contact.PreferredCurrency = Constants.ProductCatalogConstants.DefaultCurrency.ToUpper(); // String value "SEK" contact.PreferredLanguage = Constants.ProductCatalogConstants.DefaultLanguage; // String value "sv-SE" contact.SaveChanges(); } /// <summary> /// Verify existance of roles /// </summary> /// <param name="roleName"></param> /// <returns></returns> private static bool RoleExists(string roleName) { // Ensure we have a datacontext, nice to have if we are outside the web envionment if (DataContext.Current == null) { string connectionstring = ConfigurationManager.ConnectionStrings["EcfSqlConnection"].ConnectionString; Mediachase.BusinessFoundation.Data.DataContext.Current = new Mediachase.BusinessFoundation.Data.DataContext(connectionstring); } try { // If we got a SecurityContext if (SecurityContext.Current != null) { if (SecurityContext.Current.GetAllRegisteredRoles().Any()) return SecurityContext.Current.GetAllRegisteredRoles().FirstOrDefault(x => x.RoleName == roleName) != null; } // ... and if we dont if (SecurityContext.Current == null) { string ProviderName = "CustomerSecurityProvider"; SecurityContext securityContext = SecurityContext.CreateInstance(ProviderName); if (securityContext != null && securityContext.GetAllRegisteredRoles().Any()) return securityContext.GetAllRegisteredRoles().FirstOrDefault(x => x.RoleName == roleName) != null; } } catch { } return false; }