API Questions on Kentico API.
Version 6.x > API > CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
jkane-ecentricarts - 1/24/2013 11:16:47 AM
   
CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
I'm using the API outside website context from a Console Application and am trying to change the current user context to be user #70 (a global admin) instead of default public so that operations will run in context of an admin user.

The following code snippet looks like it should affect the context user but the context user remains public.

What should I do instead?
CMSContext.Init();

UserInfo user = UserInfoProvider.GetUserInfo(70); // A global Admin

// Set context values
CurrentUserInfo cui = new CurrentUserInfo(user, true);
CMSContext.SetCurrentUser(cui);
CultureHelper.SetPreferredUICulture(cui.PreferredUICultureCode);

// This is the test ...
if (CMSContext.CurrentUser.UserID != user.UserID)
throw new Exception("failed to set user");
// Throws an error because CurrentUser is still 65 (public) instead of expected #70.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 1/28/2013 12:35:31 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
Why are you trying to change the current user? In most cases you just use the UserInfo object to get the user info you need to execute the process/method you want. Unfortunately you don't explain what process or method you are trying to execute so I can't provide an example but most of the Providers will require a user instance or ID of some sort to be executed. For instance:
UserInfo ui = UserInfoProvider.GetUserInfo(53);
CustomTableItemProvider customTableProvider = new CustomTableItemProvider(ui);
You could simply check and see if the CMSContext.CurrentUser == "public user" and if so use a different instance of the UserInfo object.

User avatar
Member
Member
ctinsley-sixthavestudios - 3/20/2013 8:40:12 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
Im have the same issue trying to update the OrderStatusID using the OrderInfoProvider. I can not find a way to change the user used to execute commands with that provider. Any help would be great.

User avatar
Member
Member
JAA - 2/12/2014 11:49:09 AM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
I thought about creating a new thread, but ill just re-up this one. Same code not working for me:
            UserInfo user = UserInfoProvider.GetUserInfo(70);
CurrentUserInfo cui = new CMS.CMSHelper.CurrentUserInfo(user, false);
CMSContext.SetCurrentUser(cui);

When I bring up the debugger, the CMSContext shows the user has not changed.

Am I missing something?

User avatar
Member
Member
JAA - 2/12/2014 11:50:10 AM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
FYI I have tried both of these
CurrentUserInfo cui = new CMS.CMSHelper.CurrentUserInfo(user, false);
CurrentUserInfo cui = new CMS.CMSHelper.CurrentUserInfo(user, true);

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 2/12/2014 12:05:39 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
Hello,

Could you please try the following code:
CMSContext.SetCurrentUser(new CurrentUserInfo(cui, true));

also try to debug what is stored in your cui variable at the moment of processing.

Best Regards,
Martin Danko

User avatar
Member
Member
JAA - 2/12/2014 2:36:16 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
kentico_martind2 wrote: Hello,

Could you please try the following code:
CMSContext.SetCurrentUser(new CurrentUserInfo(cui, true));

also try to debug what is stored in your cui variable at the moment of processing.

Best Regards,
Martin Danko

No joy
UserInfo user = UserInfoProvider.GetUserInfo(70);
CurrentUserInfo cui = new CMS.CMSHelper.CurrentUserInfo(user, [true or false]);
CMSContext.SetCurrentUser(new CurrentUserInfo(cui, true));

In the debugger:
1. cui has the information for userId 70 as desired.
2. CMS.Context shows Public Anonymous User

User avatar
Member
Member
JAA - 2/12/2014 2:38:17 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
Full Code MVP

class Program
{
static void Main(string[] args)
{
CMS.CMSHelper.CMSContext.Init();
UserInfo user = UserInfoProvider.GetUserInfo(70);
CurrentUserInfo cui = new CMS.CMSHelper.CurrentUserInfo(user, true);
CMSContext.SetCurrentUser(new CurrentUserInfo(cui, true));
}
}

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 2/12/2014 4:07:35 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
Hello,

you didn't mention that you want to change it for a public user. What is the purpose of it? The main difference here is that as a public user you are not authenticated so the application can not change the context of the user as no user is authenticated so no related context is available.

Best Regards,
Martin Danko

User avatar
Member
Member
JAA - 2/12/2014 4:16:52 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
The purpose of my code was to show that SetCurrentUser is not working. Its not working in my Console App as shown, and its also not working in our Kentico solution.

Does Kentico work differently in these instances? Is the code doing something it shouldnt be doing? Is there some documentation I am not reading?

Looking at the API this should work but isnt. What should I do different?

User avatar
Kentico Customer Success
Kentico Customer Success
kentico_martind2 - 2/12/2014 5:34:51 PM
   
RE:CMSContext.SetCurrentUser(..) not reflected in CMSContext.CurrentUser
Hi,

The reason is that you are not using it in the right context. This will not work in your console application as the context of Kentico is not available in your console application.
And it will not work for the public user as you can not change the context of non-authenticated user as I've mentioned before.

Maybe try to provide us with more details what exactly do you want to achieve and I will be able to find a different solution for you.

Best Regards,
Martin Danko