a·gen·tic a·gil·i·ty

Loss of My.User.Name is not that bad

TL;DR; Explains how to access and manage user and security information in .NET applications using System.DirectoryServices.AccountManagement instead of My.User.Name.

Published on
2 minute read
Image
https://nkdagility.com/resources/2iThCwSc5Qx
Subscribe
1Imports 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:

1 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:

1Dim 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

Subscribe

Connect with Martin Hinshelwood

If you've made it this far, it's worth a conversation. Tell us where you are and what you're trying to change. Martin reads every inquiry and replies personally.

Start the conversation

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.​

ProgramUtvikling Logo

ProgramUtvikling

Schlumberger Logo

Schlumberger

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

Freadom Logo

Freadom

Akaditi Logo

Akaditi

Graham & Brown Logo

Graham & Brown

YearUp.org Logo

YearUp.org

Trayport Logo

Trayport

Jack Links Logo

Jack Links

Microsoft Logo

Microsoft

Big Data for Humans Logo

Big Data for Humans

Alignment Healthcare Logo

Alignment Healthcare

Genus Breeding Ltd Logo

Genus Breeding Ltd

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

DFDS Logo

DFDS

Illumina Logo

Illumina

Deliotte Logo

Deliotte

Lean SA Logo

Lean SA

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

Ghana Police Service Logo

Ghana Police Service

Royal Air Force Logo

Royal Air Force

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

Nottingham County Council Logo

Nottingham County Council

Washington Department of Transport Logo

Washington Department of Transport

Capita Secure Information Solutions Ltd Logo

Capita Secure Information Solutions Ltd

Jack Links Logo

Jack Links

Illumina Logo

Illumina

Kongsberg Maritime Logo

Kongsberg Maritime

Schlumberger Logo

Schlumberger

Milliman Logo

Milliman