Get Windows username of current user - C#

chris (2008-02-12 22:41:03)
41633 views
2 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

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 iconedit reply
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 iconedit reply