NavUserAccountHelper class

  • Post category:Development
  • Post comments:4 Comments
  • Reading time:5 mins read

Even though my blog queue has a lot of far more interesting todo entries waiting for their prime time, on my short flight to the Directions EMEA conference in Poznań, instead of dozing away or blank-staring through the window, I’ve decided to introduce the Microsoft.Dynamics.Nav.NavUserAccount.NavUserAccountHelper class, a part of the built-in .NET stack of Microsoft Dynamics NAV since the version 2013 R2. My friend, Eric, asked me to blog about it, so here it is, this post is for you, Eric.

For as long as I remember there was the USERID function in C/AL which allowed you to check the name of the current user. There were times when it was not particularly useful as it did not always provide all the content of the user account, but as NAV grew increasingly stronger integrated with Windows technology stack, and as we got new authentication options with NAV 2013, we needed more insight into the information related to the user account.

With NAV 2013 R2, and of course, NAV 2015, Microsoft Dynamics NAV brings a static .NET class named NavUserAccountHelper which contains some useful members to facilitate getting insight into user accounts, as well as controlling the authentication through O365 and Azure.

To declare a variable of this type, declare a DotNet variable, and then as a subtype, choose the following assembly:

image

And then choose the following type from it:

image

At this point, you have a variable which gives you access to a series of static methods (static means you don’t have to construct an instance first) to manage user authentication and related information:

image

Among the most interesting ones, I find the UserName function, which purports to be capable of converting an SID to a Windows account user name:

image

Let’s try to see if it does that:

image

And it shows this:

image

So far, so good. Just to see how smart it was, I created another Windows account to which I didn’t give any access to NAV or SQL Server, and then I wanted to see whether this function is smart enough to convert any SID to the username:

image

And it happily complies:

image

A couple of days ago, when cleaning up a system after an upgrade to NAV 2013 R2, there were a bunch of user profiles where we knew the SIDs of users, but didn’t know which user names these were, and we wanted to clean up the recycle bins for those users. This function came in handy to figure out who these users were. Funny enough, of all built-in Windows utilities, PowerShell and stuff, it was the .NET interop in NAV that proved the simplest to quickly turn a bunch of SIDs to full human-readable Windows user names.

I know, I know, it’s not that this is the most useful of all the functions of this class, but it’s very unlikely you’ll ever want to call other ones, except maybe for some purposes to check if an account is Windows, User Name or Access Control authentication, or if it is running on Azure, using one of the Is… ThisOrThat static methods.

If you are in Poznan, enjoy the conference, there will be a lot of great content there, and I am looking forward to meeting you there. And to you, Eric, thanks for all, and good luck!

Vjeko

Vjeko has been writing code for living since 1995, and he has shared his knowledge and experience in presentations, articles, blogs, and elsewhere since 2002. Hopelessly curious, passionate about technology, avid language learner no matter human or computer.

This Post Has 4 Comments

  1. Jan

    Hi Vjekoslav,

    I ran into exaclty the same problem – cleanup after upgrade if the upgrade is not run at the customer site.
    I was googling for a solution since the funktion you mentioned is not working the way it should. On my installation the system returns an error that the “GUID” format is not correct. But – the string I’m passing has the exact same format as yours above (S-…).

    Do you by chance have any idea what might solve this problem?

    1. lsc

      “S-xxx-xxx-…” is not GUID, it is a Windows Security ID (Column “Windows Security ID” in the User table) . You should problably pass the “User Security ID” of the same table which is a GUID.

Leave a Reply