Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Virtual User Login Experience Profile

I need to validate user credentials from external service, therefore I'm using the VirtualUser authentication.

  • BuildVirtualUser, checking for the roles to set to him, saving the user Profile and then login with that name.

I'm facing a problem, that everyday that i login, with the same credentials Sitecore creates a new user in Experience Profile.
What i need to change in my code to assure that, with virtual user login, Sitecore gets the old experience profile of the user?

I was thinking in creating the user in sitecore with same generic password. Instead of using the virtual user, and authenticate directly with sitecore. Is that correct?

Here's my code:

Sitecore.Security.Accounts.User user = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(sitecoreUser, true);
string roleName = @"newRole\User";
Sitecore.Security.Accounts.Role demoRole = Sitecore.Security.Accounts.Role.FromName(roleName);

if (Sitecore.Security.Accounts.Role.Exists(roleName) && !demoRole.IsMember(user, true, false))
{
    user.Roles.Add(Sitecore.Security.Accounts.Role.FromName(roleName));
}

user.Profile.Name = name;
user.Profile.Email = email;
user.Profile.FullName = fullname;

user.Profile.Save();
Sitecore.Security.Authentication.AuthenticationManager.Login(user.Name);

Tracker.Initialize();
like image 359
Ricardo Azeitona Avatar asked Oct 18 '22 01:10

Ricardo Azeitona


1 Answers

Code looks fine, but you miss one important thing: to identify your user/contact.

You need to add next line of code:

  Tracker.Current.Session.Identify(email);

Please check next link to find more information about how to identify contacts:

https://doc.sitecore.net/sitecore_experience_platform/setting_up__maintaining/xdb/contacts/identifying_contacts

like image 128
Vlad Iobagiu Avatar answered Oct 24 '22 14:10

Vlad Iobagiu