Create a User Profile and Log-in

Create a user profile and then log-in through the Sitecore API.

In your Visual Studio Solution setup:

  1. Reference the Sitecore.Commerce.Connect.CommerceServer and Sitecore.Commerce.dll.
  2. Pick the class in your solution where you would like to create and log in a user.
  3. To create the user profile, copy and paste the following code:
    var customerServiceProvider = new Sitecore.Commerce.Services.Customers.CustomerServiceProvider();
    var userName = @"CommerceUsers\testUser";
    var password = @"mY_P@ssw0rd";
    var email = @"";
    var shopName = Sitecore.Context.Site.Name;
    // create the user
    var result = customerServiceProvider.CreateUser(new Sitecore.Commerce.Services.Customers.CreateUserRequest(userName, password, email, shopName));
    if (result.CommerceUser != null)
        // if the user was created, log in the user
        var isLoggedIn = Sitecore.Security.Authentication.AuthenticationManager.Login(userName, password, false);
  4. Log in.