Worklight:WL.Server.setActiveUser - 无法修改 - 非法状态:无法更改标识

时间:2013-06-08 08:58:54

标签: session ibm-mobilefirst adapter

我正在尝试修改登录的用户身份

var mydata="this is custom data array";

var user = JSON.parse(JSON.stringify(WL.Server.getActiveUser()));
           WL.Logger.debug("Before Update" + user.attributes);
           WL.Logger.debug(" displayName =" + user.displayName );
           WL.Logger.debug("isUserAuthenticated ="+ user.isUserAuthenticated );
           WL.Logger.debug("userId =" + user.userId );

          WL.Server.setActiveUser ("myAppRealm" ,{    userId: user.userId ,
               displayName: user.displayName,
               isUserAuthenticated: user.isUserAuthenticated,
               attributes: {  userdata: mydata   }
           } );
           WL.Logger.debug(" ---- Updateed user ---- "  );
           var user = JSON.parse(JSON.stringify(WL.Server.getActiveUser())); 

给出以下异常

response [/apps/services/api/myApp/common/query] success: /*-secure-
{"isSuccessful":false,"warnings":[],"errors":["Illegal State: Cannot change identity of an already logged in user in realm 'myAppRealm'. The application must logout first."],"responseID":"67","info":[]}*/ worklight.js:1097
Procedure invocation error. Illegal State: Cannot change identity of an already logged in user in realm 'myAppRealm'. The application must logout first. 

我该如何修改?

修改

@Xv。好吧,那次我需要在用户的会话对象中保存一些值。为此,我首先尝试修改用户的对象,如上所述,但后来我发现下面提到的API有助于在会话对象中检索,修改或添加值。

WL 6.3文档:

  

访问HttpServletRequest对象。

     
      
  • WL.Server.getClientRequest
  •   

这使您可以直接访问 HttpServletRequest 对象,然后就像在JEE应用程序中一样使用它的所有方法。

例如:

  

WL.Server.getClientRequest()。的getSession()。的getAttribute( “的myKey”)   WL.Server.getClientRequest()。getSession()。setAttribute(“mykey”,myobj)

1 个答案:

答案 0 :(得分:2)

  1. 始终getActiveUser API中提供领域名称,例如WL.Server.getActiveUser("myRealm")

  2. 正如错误消息所示 - 您无法更改活动用户身份,可变。您需要做的是首先通过调用WL.Server.setActiveUser("myRealm", null)处理现有用户身份,然后调用WL.Server.setActiveUser("myRealm", {...})