Automatic User Creation / login in Commerce Server Starter Site
ravikanth | August 17, 2008In Microsoft commerce server forums, I came across one post with a nice requirement. The requirement was
- Create a user in Starter site without manual entry . ie User Details would come from Active Directory, using this details , starter site should create a user profile if he visits the site for first time.
- If an Existing user visits the Starter site, his user profile details should be fetched automatically without entering username and password.
Here is the resolution for this requirement.
Pre-requisites
- Instead of having the same profile data at two different locations (commerce server & AD), if possible, try to extend the commerce server to map the fields in AD (optional).
- By default commerce server uses email ID as primary key in User Profile. You can extend profile system to make user Id (which holds AD user ID) as primary key. To change primary key value, check below site for more details.
http://techblog.ravikanth.net/blog/cns!F348D7145D1BE6C2!459.entry - Enable windows authentication on starter site.
Authentication Process:
- Get logged in user ID in to the application. We will check whether profile session available.
- If there is no profile session available, we will get user details using WindowsIdentity class.
- Call commerce server profile system to get user profile details
- If nothing is returned, we will create a user profile for that user. We will fetch necessary information from active directory and create an entry in commerce server profile system.
Note: The password will be same for all users – say Pass@123. - Store user profile details in session for further use.
I tested this approach on starter site and it worked. For instance, for auto-login feature I had added below code in standardLayoutMaster.CS init() method.
bool isAnonymousUser = SiteContext.Current.IsAnonymousUser;
if (isAnonymousUser == true)
{
CurrentUser.RefreshCache();
WindowsIdentity identity = WindowsIdentity.GetCurrent();
string[] userName = identity.Name.ToString().Split(”);
// Set new user id.
Guid registeredUserId = (Guid)Membership.GetUser(userName[1]).ProviderUserKey;
CommerceContext.Current.UserID = registeredUserId.ToString(”B”);
SiteSecurity.CreateUserCookie();
LogCommerceEvent.UserSignIn(registeredUserId);
}
Every time, application checks whether the current user is anonymous user. If yes, it will get user name (remove domain/system name) from windows identity and pass it to commerce server to get user Id (guid). This GUID is assigned to commerce server context.