Skip to main content

Getting user First Name and Last Name from Active Directory

Recently, I had to create a asp.net MVC portal for organizing test messages. Since site is personalized for User, we were required to fetch and disply username, when they login. This being intranet site, user is authenticated using Windows Authentication. Windows user names are most of the time are mix of literal, not conveying users First and Last name. I used following code to get User First and Last name, using DirectorySearcher class to query Active Directory. Since user name in HttpContext is in format DomainName\\UserName, I had to extract user name alone and use it filter.


using System.DirectoryServices;
...
...
DirectorySearcher ds = new DirectorySearcher();
var indexOfBS = User.Identity.Name.IndexOf("\\");
ds.Filter = String.Format("((SAMAccountName={0}))", User.Identity.Name.Substring(indexOfBS + 1, User.Identity.Name.Length - indexOfBS - 1));
ds.PropertiesToLoad.Add("givenName");
ds.PropertiesToLoad.Add("sn");
var directoryEntry = ds.FindOne();
var givenName = directoryEntry.Properties["givenName"][0];
var surName = directoryEntry.Properties["sn"][0];
string userName = String.Format("{0}, {1}", surName, givenName);

Comments

Popular posts from this blog

BizTalk Deployment Framework fix for compatibility issues with BizTalk 2016

Objective My objective was to modify BizTalk Deployment framework source, with the intend of getting it to work with BizTalk 2016, as it does not work out of the box with BizTalk 2016.
Source Location BizTalk Deployment Framework source is located at CodePlex and downloaded from below link https://biztalkdeployment.codeplex.com/SourceControl/latest It has BTDF 6.0 under dev folder and BTDF 5.0- 5.6 under release folder. BTDF 5.6 release version is more suited, as it is a stable release, hence it is modified to suit our need. All the references going forth are from the 5.6 release folder.
Modifications Below is the modification that were done in order to get BTDF 5.6 release version, working.    1.Added copy of BizTalk 2016 SSOClient to \Tools\SSOSettingsFileImport\ExternalReferences\BTS2016\Microsoft.BizTalk.Interop.SSOClient.dll, we will use as reference later. 2.In solution, \Tools\SSOSettingsFileImport\SSOSettingsFileImport.sln clone projects SSOSettingsEditorBTS2013R2, SSOSettingsFile…

Undoing the mistake of selecting Sticky Notes as notes keep

Sticky Notes seems straight forward means to keep notes that you need handy, like quick notes on command you use frequently. Unfortunately, once your notes reaches certain number, the program starts to lag in response i.e. your click or select will not register and you end getting frustrated. I been there and I resorted to copying and pasting notes from sticky notes to more awesome OneNote. I soon got tired of unresponsive sticky note and resorted to finding where the data is stored and how to retrieve it.

Following are my finding(or googling),
1. In Windows 10, Sticky notes stores data at %LocalAppData%/Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState and inside file with extension *.sqlite.
2. Now to open the file we can use windows tools under sqlite.org download page. I used sqlite3.exe
3. Type in following commands after getting to folder where sqlite3.exe exists,

sqlite3.exe - This is to get to sqlite prompt.excel - This is to specify that following command shoul…