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.
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
}
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
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);
}
}
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();
}
}
}
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:
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?