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 = "[email protected]";
   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.

14
  • virtual user exists in a single session. are you sure that none of your code is clearing it? Commented Apr 17, 2017 at 13:54
  • No Andrey, we are not clearing any session values. Also, currently we are doing this on a single server instance. Commented Apr 17, 2017 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.sstatic.net/iBafP.jpg) Commented Apr 18, 2017 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. Commented Apr 19, 2017 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. Commented Jun 13, 2017 at 19:19

3 Answers 3

5

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
  • 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
    Commented Jun 19, 2018 at 5:40
  • @WulfgarDK I haven't got any fix from Sitecore Commented Nov 21, 2018 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.

-5

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.

2
  • 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. Commented Jun 12, 2017 at 22:26
  • please provide detailed explanation Commented Nov 21, 2017 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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