I am trying to login a virtual user for my application and have assigned roles to this virtual user.
var virtualUser = Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(userName, true);
Sitecore.Security.Accounts.Role itemRole = Sitecore.Security.Accounts.Role.FromName(role.ToLower());
virtualUser.Roles.Add(itemRole);
Sitecore.Security.Authentication.AuthenticationManager.LoginVirtualUser(virtualUser);
Now I want to logout from this Virtual User so I have used
Sitecore.Security.Authentication.AuthenticationManager.Logout();
It works fine until here.. but when I login to this virtual user again. It persists the roles that were previously assigned to it. how can I remove assigned roles from the virtual user before I log the user out? I have tried removing all roles before logout but that didn't work.
Sitecore.Context.User.Roles.RemoveAll();
Sitecore.Security.Authentication.AuthenticationManager.Logout()
Other approaches I have tried which didn't work.
var virtualuser = Sitecore.Security.Authentication.AuthenticationManager.GetActiveUser();
Sitecore.Security.Authentication.AuthenticationManager.Logout();
virtualUser.Delete();
and
Sitecore.Security.Authentication.AuthenticationManager.Logout();
HttpContext.Current.Session.Abandon();
What should be done to remove roles and logout the virtual user?
Update: I am using extranet\authenticated
as the username to login as a virtual user. Could this be causing the issue.
Sitecore Version 9.0.2