Winforms: how to get windows user first name

Hi,

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

Thanks.

quasar_eeAsked:
Who is Participating?
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.

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


OR

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

Open in new window


OR

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

Open in new window


OR

 
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

Regards,

Asif Ahmed Khan
0
khan_webguruCommented:
Hello,

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

Regards,

Asif Ahmed Khan
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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.
0
khan_webguruCommented:
Also

System.Environment.UserName;
0
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.
Thanx
0
angus_young_acdcCommented:
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

0

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:
Thanks.
I didn't use exactly the same code though:

Environment.UserDomainName, Environment.UserName
PrincipalContext, UserPrincipal, GivenName
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.