Winforms: how to get windows user first name


I'd lke to know how to get windows user first name from .NET Win Forms application.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

First Step Setting  Up Web Server

To get the windows user name through programatically. Compulsary we need to set the web server.

Open the Internet Infomation Services(IIS), Select your ASP.NET Virtual Directory, right click, go to the Properties, In Properties window go to Directory Security Tab, Here You Find the "Edit" button in Anonymous access and Authentication Control. Press Edit Button. Now Authentication Methods dialogbox will appear.

In this dialog box by default it is set(checked) Anonymous Access. uncheck this checkbox and go to botom of this dialog box check the Integrated windows authentication. Here why we are selecting this Default web server is logged by ISUER_MachineName so if it is window authentication web server will take the credentials from windows logged user.

Getting the User Name :

using three ways we can get the User Name using C#

System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;

string strName = p.Identity.Name;

Open in new window


string strName = HttpContext.Current.User.Identity.Name.ToString();

Open in new window


string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name

Open in new window


string strName = Request.ServerVariables[5]; //Finding with index

Open in new window

In Above 3 Cases returnin string contains DomainName\WinNTLoggedUserName

(for Ex: Microsoft\Bill.Gates. Here Microsoft is domain Bill.Gates is Logger User Name )

Using string operations seperate the DomainName and UserName.

Hope this will help you


Asif Ahmed Khan

If you need to retrieve the username and domain of the logged-in user in your .Net application, you can access it in a comple of simple steps. First, include the System.Security.Princial namespace at the top of your class file. eg

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
using System.Security.Principal;  // here is the security namespace you need

Open in new window

You then call the getCurrent() method against WindowsIdentity, and then reference it's Name property like so:

this.nametext = WindowsIdentity.GetCurrent().Name;

That will return the name you need, so in my case it looks like 'DSclacy'

If you want to retrieve just the username without the domain portion and the '' delimeter, you can use a String.split() method in the same line of code like this:

this.nametext = WindowsIdentity.GetCurrent().Name.Split('')[1];

Open in new window

So in the example above that would simply return 'clacy'

Hope this will help you


Asif Ahmed Khan
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
The three links I posted are only for winform applications. They should lead you to where you want to go. If you really wanted web stuff, then Mr Khan's suggestions are valid.

quasar_eeAuthor Commented:
Hi guys, I'd like to mention that we need user FIRST name (like john, david etc.) not system UserName like jsmith.
Hi there,

You could try the following to get the first name
string servername = Environment.UserDomainName;
            string username = Environment.UserName;

            using (DirectoryEntry domain = new DirectoryEntry(string.Format("WinNT://{0}/{1}", servername, username)))
                string[] names = domain.Properties["FullName"].Value.ToString().Split(' ');
                string firstName = names[0];

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
quasar_eeAuthor Commented:
I didn't use exactly the same code though:

Environment.UserDomainName, Environment.UserName
PrincipalContext, UserPrincipal, GivenName
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.