Loss of My.User.Name is not that bad…

Audience

Everyone

Imports System.DirectoryServices.AccountManagement

In WPF development you can’t just use the “My.User.Name” classes as it is not set by default. If you still want to use it you can “My.User.InitializeWithWindowsUser()” to have it setup, but there is a better way.

If you are using Active Directory .Net 3.5 has provided a new way to access information…

There is a new assembly called “System.DirectoryServices.AccountManagement” that encapsulates all of the logic for accessing security information in a number of ways.

You can create a link to your local Active Directory  by calling:

 Dim ctx As New PrincipalContext(ContextType.Domain)

The options for conencting are:

  • ApplicationDirectory – The application directory store. This represents the AD LDS store.
  • Domain – The domain store. This represents the AD DS store.
  • Machine – The computer store. This represents the SAM store.

These options allow for most security authentications in your .NET applications, and it does not stop with wrapping the top level AD. if you want to access and control a user you need only:

Dim u As UserPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.Sid, WindowsIdentity.GetCurrent.User.Value)

I really like this as often I have has to build and Google (more the later than the former) class library for manipulating Active Directory objects, and you need not just use the Sid. You can use:

  • DistinguishedName – The identity is a Distinguished Name (DN).
  • Guid – The identity is a Globally Unique Identifier (GUID).
  • Name – The identity is a name.
  • SamAccountName – The identity is a Security Account Manager (SAM) name.
  • Sid – The identity is a Security Identifier (SID) in Security Descriptor Definition Language (SDDL) format.
  • UserPrincipalName – The identity is a User Principal Name (UPN).

And this is just the direct load of a user object. There are Group object as well as fill search capability with the PrincipalSearcher and various other bits and bobs…

Well thats my find of the day smile_nerd

 

Technorati Tags:   

Create a conversation around this article

Share on Facebook
Share on Twitter
Share on Linkdin

Read more

Martin Hinshelwood
In organizational development and team dynamics, Agile (as the Agile Manifesto delineates) and Scrum (as the Scrum Guide outlines) guide teams not by solving their problems but by illuminating the issues that demand attention. These frameworks aim to identify and spotlight the challenges within a team or organization’s processes, effectively …
Martin Hinshelwood
This week, I participated in a Scrum.org Webinar hosted by Sabrina Love (Scrum.org Product Owner) as well as my colleagues, Joanna Płaskonka, Ph.D. and Alex Ballarin to discuss the state of learning and how immersive learning is the future of training. You can watch the video below to hear what …
Martin Hinshelwood
For a long time now I have been searching for that perfect domain that epitomised the vision, the why, of what I am trying to achieve with my customers and the industry at large. Now I have found it in http://nkdagility.com
Martin Hinshelwood
At the MVP Summit I was appalled by the number of people who asked questions about new features for supporting hierarchical tasks! I shared a disgusted look with Peter Provost and we had a quick (and I mean really quick) conversation that resulted in this post. it really comes down …