runAs Function

Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

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,
    str partition
    ])

Parameters

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.

partition

The partition key, of the type returned by the getCurrentPartition function; optional.

Return Value

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

Remarks

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 debugger might ignore breakpoints that are located in a method that is called by using the runAs function.

X++ code that is executed by the runAs function must run as .NET Framework CIL. If CIL has not been generated for the target static method, an error message indicates that the method is not found.

The PartitionKey system type is the exact type of the partition parameter. PartitionKey is a string with a maximum length of 8 characters. The partition parameter was added in Microsoft Dynamics AX 2012 R2.

Example

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 public 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();
    }

See also

GlobalrunClassMethodIL()

Functions

Announcements: To see known issues and recent fixes, use Issue search in Microsoft Dynamics Lifecycle Services (LCS).