blog

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

Published on
2 minute read
Imports System.DirectoryServices.AccountManagement

 

Loss of My.User.Name is not that bad...   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:

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:

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 Loss of My.User.Name is not that bad...

Technorati Tags: .NET    WPF 

code-and-complexity blog code develop wpf

Related blog posts

No related videos found.

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

YearUp.org Logo
Ericson Logo
Brandes Investment Partners L.P. Logo
Xceptor - Process and Data Automation Logo
Cognizant Microsoft Business Group (MBG) Logo
ALS Life Sciences Logo
Workday Logo
Graham & Brown Logo
Genus Breeding Ltd Logo
Higher Education Statistics Agency Logo
Schlumberger Logo
Freadom Logo
Flowmaster (a Mentor Graphics Company) Logo
Boeing Logo
Akaditi Logo
Bistech Logo
SuperControl Logo
Philips Logo
Royal Air Force Logo
Nottingham County Council Logo
Department of Work and Pensions (UK) Logo
Washington Department of Enterprise Services Logo
New Hampshire Supreme Court Logo
Ghana Police Service Logo
Brandes Investment Partners L.P. Logo
Epic Games Logo
Teleplan Logo
Kongsberg Maritime Logo
Akaditi Logo
Lean SA Logo