2

We used the below code to virtually login the user to the Sitecore version 9.2

Sitecore.Security.Accounts.User virtualUser = AuthenticationManager.BuildVirtualUser("extranet\\user1", true);
virtualUser.Profile.Save();
AuthenticationManager.LoginVirtualUser(virtualUser);

But it's failing and throwing below error, is there any known issue/workaround for virtual login in Sitecore 9.2.

Note - Referred all the posts related to virtual login in this forum but nothing works out.

{
    "Message":"An error has occurred.",
    "ExceptionMessage":"UserId not found.",
    "ExceptionType":"System.InvalidOperationException",
    "StackTrace":"at 
        Microsoft.AspNet.Identity.UserManager`2.d__42.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
        at Microsoft.AspNet.Identity.ClaimsIdentityFactory`2.d__0.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at Sitecore.Owin.Authentication.Identity.MembershipUserManager.d__8.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at Sitecore.Owin.Authentication.Identity.ApplicationSignInManager.d__18.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at Microsoft.AspNet.Identity.Owin.SignInManager`2.d__2.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at Sitecore.Owin.Authentication.Identity.ApplicationSignInManager.d__19.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func)  
        at Sitecore.Owin.Authentication.Security.AuthenticationManager.Login(User user)   
        at VirtualAuthController.UserLogin() in C:\\Controllers\\VirtualAuthController.cs:line 50   
        at lambda_method(Closure , Object , Object[] )   
        at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c__DisplayClass6_2.b__2(Object instance, Object[] methodParameters)   
        at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Web.Http.Filters.ActionFilterAttribute.d__6.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---   
        at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()  
        at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)   
        at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()"
    }

Update - Issue is only in Sitecore 9.2 the same code is working in Sitecore 9.1

3
  • Is it crashing on save or LoginVirtualUser?
    – josedbaez
    Mar 3, 2020 at 14:26
  • 1
    Did you find any solution on this error? I have the exact same error on Sitecore 9.2. It is crashing on the LoginVirtualUser method May 25, 2020 at 12:25
  • It seems like a defect.. not able to get a patch, so as an alternate moved to form authentication as a temp work-around on CDS servers. May 25, 2020 at 13:32

4 Answers 4

1

Had the same issue on Sitecore 9.1. I contacted Sitecore Support, there was a KB article which was detailing performance issues when multiple virtual users log in quickly after another.

Link to KB article: https://kb.sitecore.net/en/Articles/2020/03/11/14/01/132937.aspx

Using the .Net stopwatch, I saw login times reduced from 7-10 seconds to 100ms.

1
  • We were having this exact issue where no users were able to log in suddenly. Applied the hotfix resolved the issue for us Sep 3, 2020 at 14:05
0

I use the below code to create and login the virtual user. The code is part of a helper class and accepts username ,displayname arguments.

 //Create virtual User
                User virtualUser = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(userName, false);

                virtualUser.RuntimeSettings.Load();
                virtualUser.RuntimeSettings.AddedRoles.Clear();
                virtualUser.Roles.RemoveAll();

                // Optional: Add the user to an existing extranet role
                virtualUser.Roles.Add(Role.FromName("AzureAdB2C\\"));

                // set profile properties
                virtualUser.Profile.Email = userName;
                virtualUser.Profile.Name = displayName;
                virtualUser.Profile.FullName = displayName;
                virtualUser.Profile.Comment = "User Created from Some IDS";

                virtualUser.Profile.Save();
                virtualUser.RuntimeSettings.IsVirtual = true;

                virtualUser.RuntimeSettings.Save();
                user = virtualUser;
                _res = true;
                virtualUser.Profile.Reload();

                // Login the virtual user
                Sitecore.Security.Authentication.AuthenticationManager.LoginVirtualUser(virtualUser);  
4
  • I guess this is similar to my code above and getting the same error.. is this working in Sitecore 9.2.? Because my code works in earlier versions till 9.1 Feb 19, 2020 at 14:36
  • no i worked on 9.1 with this
    – Abhay Dhar
    Feb 20, 2020 at 21:18
  • 1
    Reach out to support i guess
    – Abhay Dhar
    Feb 20, 2020 at 21:19
  • i think that might be my take away here, because it works in 9.1 but not in 9.2 Feb 27, 2020 at 21:48
0

Think this should work, you can give it a try

var virtualUser = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser("sitecore\\demo@demo.com", true);
  //virtualUser.Profile.IsAdministrator = true;
  virtualUser.RuntimeSettings.IsAdministrator =true;
  Sitecore.Security.UserProfile profile = virtualUser.Profile;
  profile.IsAdministrator = true;
  profile.Save();
  Sitecore.Security.Authentication.AuthenticationManager.LoginVirtualUser(virtualUser);
3
  • I don't think that this line is proper profile.IsAdministrator = true; because my virtual users are not admin. Mar 3, 2020 at 13:18
  • No but it is also comment out or you can delete it if you want but then remember to add relevant roles if you need to go into the Sitecore client Mar 3, 2020 at 14:00
  • okay got.. it anyways fails in 9.2 Mar 3, 2020 at 16:04
0

After setting the following settings to false the error is gone:

  • Owin.Authentication.Enabled
  • FederatedAuthentication.Enabled

Caution: on CM, this disables the default authentication which could cause problems, although you can disable on CD servers if you don't need Owin authentication.

Sitecore provides example patch files which you can enable to disable identity server:

  • App_Config\Include\Examples\Sitecore.Owin.Authentication.Disabler.config
  • App_Config\Include\Examples\Sitecore.Owin.Authentication.IdentityServer.Disabler.config

Your Answer

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

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