runAs Function

Enables the caller to run an X++ method in the security context of another user. This function is most often used with batch processing.


container runAs(
    str userId,
    int classId,
    str staticMethodName,
     [container params,
    str company,
    str language])

Parameter

Description

userId

The Microsoft Dynamics AX user to be impersonated.

classId

The class to be invoked in the impersonated session.

staticMethodName

The class method to be invoked in the new user context.

params

The parameters to be passed to the method; optional.

company

The company selected for the impersonated session; optional.

language

The language selected for the impersonated session; optional.

The container that holds the return value or values, if any, of the method called by the runAs function.

This function makes it possible to run code as another Microsoft Dynamics AX user. This presents a security threat. Therefore, this function runs under Code Access Security. Calls to this function on the server require permission from the RunAsPermission Class. Each use of this API should be threat-modeled. If a security vulnerability is discovered, validate input to this API.

The following example calls the runDueDateEventsForUser method in the EventJobDueDate class. The code runs in the security context of a Microsoft Dynamics AX user. Run this code by applying it to a method in a new class.

server static void main(Args args)
{
    RunAsPermission perm;
    UserId          runAsUser;
    SysUserInfo     userInfo;
    ;

    userInfo = SysUserInfo::find();
    runAsUser = userInfo.Id;
    perm = new RunAsPermission(runAsUser);
    perm.assert();

    RunAs(runAsUser, classnum(EventJobDueDate), "runDueDateEventsForUser");

    CodeAccessPermission::revertAssert();
}

Community Additions

ADD
Show: