5

We have created a Virtual User in Sitecore (to reside in Sitecore memory) - we are able to see the virtual user with all the profile data we save but on a redirect to a new page, the Virtual user and its data is lost. We are just seeing a normal anonymous user which is not a virtual user.

The virtual user is created using the below code syntax -

Sitecore.Security.Accounts.User user = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(@"domain\user", true);
if (user != null)
{
   string domainRole = @"domain\role";
   if (Sitecore.Security.Accounts.Role.Exists(domainRole))
   {
     user.Roles.Add(Role.FromName(domainRole));
   }

   user.Profile.Email = "user@domain.com";
   user.Profile["Custom Property"] = "Custom Value";
   user.Profile.Save();
   Sitecore.Security.Authentication.AuthenticationManager.LoginVirtualUser(user);
}

Are we missing something to make the virtual user available on all pages of our application.

  • virtual user exists in a single session. are you sure that none of your code is clearing it? – Andrey Bobrov Apr 17 '17 at 13:54
  • No Andrey, we are not clearing any session values. Also, currently we are doing this on a single server instance. – Amit Gaurav Apr 17 '17 at 14:59
  • Do you see something related to this in the log files? Do you see the session cookie? ![enter image description here](i.stack.imgur.com/iBafP.jpg) – Dennis de Laat Apr 18 '17 at 7:20
  • No Dennis - nothing specific to sessions in log. Moreover i have found a weird think happening - the Virtual User is persisting for a Anonymous user but for a Logged-in user its not persisting. – Amit Gaurav Apr 19 '17 at 11:25
  • Are you trying this master databse or web db? I faced this issue on the master db (during dev phase) but not on the web db. – sc.kp Jun 13 '17 at 19:19
4

I recently came across with the same situation where we have created Virtual user and also created some custom property. At the early stage it was working properly as Sitecore user works but one day suddenly it stopped working.

In my solution still Sitecore.Context.User.IsAuthenticated this was always true but all the profile properties was empty at this time. Then I started digging into our latest change we made for project and found we just added a view rendering on LogIn page (Login Item). And that rendering was having

var customPropValue = Sitecore.Context.User.Profile.GetCustomProperty("customProp");

code and some other code too.

To just test I removed the rendering from the login item and it seems functionality again started working because after login it was either redirecting me to home page or RedirectURL page.

Then it was proved that Virtual user was not working because of the new rendering which contains code to get custom property of user. I again applied that rendering to item and this time I removed all other code from the rendering and still keep

var customPropValue = Sitecore.Context.User.Profile.GetCustomProperty("customProp");

code into it and this time again issue was same I lost all the profile properties of virtual user.

At last I removed the

  var customPropValue = Sitecore.Context.User.Profile.GetCustomProperty("customProp");

code from the rendering and it started working for me.

I want that code on other pages but not on login page so I added a condition on top if page is login page then don't execute this code and execute for all other pages. After making this change it started working for me.

On the other pages I was using same code so there was no issue with the code I found that issue is with the code on Login page. After login and redirect user on any page everything works perfect in virtual user.

I already opened a ticket in Sitecore for same they are into investigation why it is not working if we are getting any profile properties at the time of login.

We have tested this Virtual user functionality with multiple login at the same time and honestly it is working perfect. So I would say the Virtual feature is the really cool feature provided by Sitecore but on one point we need to be more careful. I hope Sitecore will resolve this issue in next releases or will provide patch for same.

  • 2
    Thanks, you saved my life with this, we had custom properties disappearing across different environments at different times with the same code, and all because of trying to extract a custom property in a menu before possibly being logged in. Did you ever get a fix from Sitecore or other explanation? – WulfgarDK Jun 19 '18 at 5:40
  • @WulfgarDK I haven't got any fix from Sitecore – Yogesh Sharma Nov 21 '18 at 11:07
-1

I had the same problem. In my case it was multiple domains. I created a virtual user at domain1.test.local and expected it at domain2.test.local. That doesn't work because the ASP.NET session cookie is domain specific.

-4

Try to clear the cache by using below code before creating user.

if (Sitecore.Context.ClientData != null)
{
    var ClientDataValues = Sitecore.Context.ClientData.GetValueNames();
    if (ClientDataValues != null && ClientDataValues.Any())
    {
        var KeyName = "SC_USR_" + userName;
        var clientDataValue = Sitecore.Context.ClientData.GetValue(KeyName);
        if (clientDataValue != null)
        {
            Sitecore.Context.ClientData.RemoveValue(KeyName);
        }
    }
}

Give it a try and let me know.

  • Can you add some details regarding what this code is doing, the possible outcomes you are expecting and what you hope this code will accomplish? If you consider this a production-ready solution wherein you always evict the user from cache on each request, please explain why this will not adversely affect performance or other native logic. – Zachary Kniebel Jun 12 '17 at 22:26
  • please provide detailed explanation – Andrei Paliakou Nov 21 '17 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.