Registering a new Commerce User & applying roles

Vote:
 

When registering a new Commerce user, do we also need to register them in the CMS?

At present I have something similar to the below for registering a new user in Commerce:

        public bool RegisterCommerceUser(string username, string email, string password)
        {
            var emailExistsAlready = CustomerContact.FieldEmail.Contains(email);
            if (emailExistsAlready)
            {
                return false;
            }
            var customer = CustomerContact.CreateInstance();
            customer.Email = email;
            customer.Password = password;

            customer.SaveChanges();

            return true;
        }

But do I then need a separate method to register them in the CMS using ASP.NET Identity? Or, does a Commerce user not need any presence in that database?

Moreover, once registering a customer for Commerce, how can I assign roles to the customer?  

#280372
May 16, 2022 9:44
Vote:
 

You'll need to create a user before you create a customercontact against it.

Assuming you've got a identity setup in the middleware then you can inject UIUserProvider and call CreateUser once successfully then create the CustomerContact as you are doing in your own example.

#280377
May 16, 2022 11:27
Vote:
 

Thanks @Surjit.

How do the users created in ASP Identity sync with users registered in CustomerContact? Is it simply doing a query to bring out matching emails?

Will I need two methods once a user registers? 

RegisterCmsUser(string username, string email, string password)
{
   // use UIUserProvider to regsiter user in the CMS
}

RegisterCommerceUser(string username, string email, string password)
{
   // Use the CustomerContact object to register a user in Commerce
}
#280378
Edited, May 16, 2022 11:34
Vote:
 

I believe its matched by the Identity Name, the Contact would store this as the user id.

The CustomerContact should get initialized when the user logs in. You can grab it from CustomerContext.CurrentContact

When you create your identity user, you should get returned a IPrinciple object. Use that to pass it into CustomerContact.CreateInstance(user) and that will assign the id fields for you

#280383
May 16, 2022 12:33
Vote:
 

Surjit, I know this is an old post but I was hoping you could help.

I have got to the stage now where I need to register a user in Commerce so have revisited your last response.

In the `UIUserProvider` class there is a `CreateUserAsync()` method which returns a `CreateUserResult`, not an `IPrincipal`. With this in mind, how do you then run the `CustomerContact.CreateInstance(user)` code to then register the user in Commerce?

I have some sample code below to make my point clearer.

        private async Task CreateUser(string Username, string Email, string Password, IEnumerable<string> roles)
        {
            var result = await _uIUserProvider.CreateUserAsync(Username, Password, Email, null, null, true);
            if (result.Status == UIUserCreateStatus.Success)
            {
                foreach (var role in roles)
                {
                    var exists = await _uIRoleProvider.RoleExistsAsync(role);
                    if (!exists)
                    {
                        await _uIRoleProvider.CreateRoleAsync(role);
                    }
                }

                await _uIRoleProvider.AddUserToRolesAsync(result.User.Username, roles);

               // now I would like to register the user in the Commerce database
               // however the code fails to compile as 'result' is it is a 'CreateUserResult' not an IPrincipal
               CustomerContact.CreateInstance(result);

                var resFromSignIn = await _uISignInManager.SignInAsync(Username, Password);
            }
        }
#283498
Jul 11, 2022 11:44
Vote:
 

I believe I have resolved this now, see code below. Specifically the use of the IPrincipalAccessor

      private async Task CreateCommerceUser(string Username, string Email, string Password, IEnumerable<string> roles)
      {
         var result = await _uIUserProvider.CreateUserAsync(Username, Password, Email, null, null, true);
         if (result.Status == UIUserCreateStatus.Success)
         {
            foreach (var role in roles)
            {
               var exists = await _uIRoleProvider.RoleExistsAsync(role);
               if (!exists)
               {
                  await _uIRoleProvider.CreateRoleAsync(role);
               }
            }

            await _uIRoleProvider.AddUserToRolesAsync(result.User.Username, roles);
            var resFromSignIn = await _uISignInManager.SignInAsync(Username, Password);
            if (resFromSignIn)
            {
               // now create the contact in Commerce
               var principal = ServiceLocator.Current.GetService<IPrincipalAccessor>();
               var contactDatabase = CustomerContact.CreateInstance(principal);
               contactDatabase.SaveChanges();
            }
         }
      }
#283504
Jul 11, 2022 13:19
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.