Get Windows username of current user - C#

chris (2008-02-12 22:41:03)
334337 views
10 replies

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

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];

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


christo

Digg it! Submit to Slashdot Add to Blinklist Del.icio.us Add to Newsvine Add to Technorati Add it to Google Bookmarks
comment
saranya
2008-06-11 11:45:57

thanks

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

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];

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


christo

reply icon
Victor Villalobos
2009-08-10 18:41:17

Thanks


It works wonderful thanks...

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

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];

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


christo

reply icon
Trupti
2010-09-01 19:32:28

Is there any method to get password for the user you are logged in?

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

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];

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


christo

reply icon
Jon
2012-03-18 00:07:09

this.nametext = WindowsIdentity.GetCurrent().Name.Split('\\\\')[1];
reply icon
Ryba
2012-03-15 10:48:20

Too difficult

If you need current logged user / domain just use Environment.UserName and Environment.UserDomainName
reply icon
anonymous
2011-08-05 20:54:35

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

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];

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


christo





what is nametext in this solution?
reply icon
anonymous
2011-09-14 13:49:52

Is there any method to get password for the user you are logged in?

No chance! That would break every security principle in the book!
reply icon
Alva
2011-12-07 00:24:31

Thanks for the namespace

Thanks for this. Too many people who put up C# examples don't include the namespace. 9 times out of 10 all that I need to solve my problem is to know the correct name space.
reply icon
dg
2011-11-10 18:09:03

Thanks!

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

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];

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


christo

reply icon
anonymous
2011-11-10 18:08:32

Thanks!

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

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];

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


christo

reply icon