Create and set a current user session

josh_brostoff
Giga Contributor

I have a scripted REST API where I want to create a new user session based off the user name (line 4) in the request header.   However, because this integration does not use any type of authentication, the current user is identified as "guest".   I want to set the current user to the user session that is created in my gliderecord query below.   How can I do this?

//create a user session

  var gr = new GlideRecord('sys_user_session');

  gr.initialize();

  gr.name = sn_user_name;

  gs.info('this is the username for the user session' + sn_user_name)

  gr.insert();

  gs.info('current user session GlideRecordSecure query: {0}', gs.getUserID());

8 REPLIES 8

I think inserting the record into the 'sys_user_session' will not create the new user session. This will track the logged in user sessions. To create a new user session we have to use both username and password.


If you have not solved this I think you are going to have to use Digest auth to make this happen.


ujjwalmahajan
Tera Contributor

Use the following code to execute block of script with some user not with the guest.

 

var new_user = 'sys_id of user';

session.onlineImpersonate(new_user);//system will execute following script with new_user user.

 

<your_code>


session.onlineUnimpersonate();

Kalyan Vallamse
Tera Contributor

Hi All,

I would like to do the same in a scoped app, I see that the above solution only works in a global scope.

gs.getSession().impersonate('161ce02e4f166640e11fe3414210c76b');// works in global scope but not in application scope.
https://community.servicenow.com/community?id=community_question&sys_id=66aebe65db58dbc01dcaf3231f961941

Kal