Friday 13 December 2013

Quick Tip #1: Debug with reduced permissions

I discovered this neat trick when troubleshooting Security issues in AX 2012.

I’m not sure about you, but finding issues related to restricted security for a user is sometimes like looking for a needle in a haystack. Suffice to say that this often leads to users having more permissions than they should.

It would be nice if security related failures were at least logged somewhere, but currently they can present themselves in many different forms, often disguising the real issue.

Debugging code can be one way to troubleshoot security related issues, and is often a last resort. However if you remove yourself (or the user you are testing) from the System Administrator group, you cannot debug. And even if you didn't need to debug, it is inefficient to say the least. So here's how you can have your cake and eat it too.

How to steps

  1. Start AX and modify the security for the user you are logged in as. This user must be a member of the system administrator role.
  2. Open a developer workspace (Ctrl + Shift + W)
  3. Ensure you set breakpoints in the code that you wish to debug.
  4. Create a job and run the following code:
    SecurityUtil::sysAdminMode(false);
    
  5. Open an application workspace from the development workspace (Ctrl + W)
  6. Perform the task you are troubleshooting and the debugger will start when it encounters a breakpoint.
To revert to system administrator privileges, close out of AX and then launch the application how you normally would. Or alternatively, Change the code in your job to:

SecurityUtil::sysAdminMode(true);

and run. This will put the session back in admin mode. Just open an application workspace from the development workspace and you once again have full admin privileges. 

More information about this can be found in the Microsoft Dynamics AX 2012 White Paper: Role-based Security Use Patterns for Developers.

No comments:

Post a Comment