API
Version 7.x > API > Sessions. View modes: 
User avatar
Member
Member
paragbaji - 11/28/2013 5:07:32 AM
   
Sessions.
Dear Team,

Two Queries for you:

1.
I need to fetch session values from CMS_Sessions table from the Kentico Database.

So far, I have seen APIs to get and set Session values directly from code behind...

Please guide me for the above.

2.

I also have tried... SessionsManager.CurrentSession.SessionUserName.ToString();

My question here is does the SessionUserName in this context comes from CMS_Session table?

Please help.

User avatar
Kentico Legend
Kentico Legend
Brenden Kehren - 11/28/2013 5:54:10 PM
   
RE:Sessions.
Why do you need to get them directly from the database table? Have you used the SessionInfo() class and SessionManager to get the info you need?
SessionInfo si = SessionManager.CurrentSession;

User avatar
Member
Member
paragbaji - 11/28/2013 11:23:49 PM
   
RE:Sessions.
Thank you for your reply, seems that I am close enough.

Now, as per your instructions, I have created that si object.

I want to get the si objects SessionUserName.

But it throws, NullReferenceException: Object reference not set to an instance of an object.
---------
My code snippet for your reference:
SessionInfo si = SessionManager.CurrentSession;

string UsrName = si.SessionUserName.ToString();

[and a code behind alert showing UsrName value]

PS: This happens when I preview it in live mode. Normally, the alert would work showing Administrator name.

Should I use
CurrentUser.Username(); 

Does that has to do something regarding sessions?

Or for
si.SessionUserName.ToString();
I need to pass hardcoded values?

Please guide me.

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 11/29/2013 3:13:13 AM
   
RE:Sessions.
Hi,

Thank you for your message.

Is the session really what you are looking for? From your description it seems like you want to get the current user name, is that correct?

If this is the case the CMSContext object is holding these information and you can pull them easily. E.g. to get current user name you can use:
string CurrentUserName = CMSContext.CurrentUser.UserName;

Let me know how that works.

Kind regards,
Richard Sustek