![]() |
Languages »
C# »
General
Intermediate
Get The User Name In C# For NT AuthenticationBy Nick ParkerThrough the .NET Framework you can easily get the current user name to authenticate. |
C#.NET 1.0, Win2K, WinXP, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
Our applications are done in Visual Basic, so we of course have to make a Windows API call where we have defined the following function:
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
This is not difficult at all, however I have been playing around with C# lately and I thought it would be interesting to find out how we will do this in managed code.
My first idea was to do an import of the above mentioned advapi32.dll along with the method we will use to get the user name. There have been many other examples on Code Project where users have imported methods through existing .dll's, so hopefully some of this looks familiar. I have tried to document what is going on here, even though it is rather simple. The following code will require you to add using statement as well.
//====================================================
//Include at the top, above the namespace declaration.
using System.Runtime.InteropServices;
//=====================================================
//Defines the .dll file to import as well as the method
//to use.
[DllImport("Advapi32.dll", EntryPoint="GetUserName",
ExactSpelling=false, SetLastError=true)]
//====================================================
//This specifies the exact method we are going to call
//from within our imported .dll
static extern bool GetUserName(
[MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
[MarshalAs(UnmanagedType.LPArray)] Int32[] nSize );
//===================================================
//The following is the implementation of our imported
//method.
byte[] str=new byte[256];
Int32[] len=new Int32[1];
len[0]=256;
GetUserName(str,len);
MessageBox.Show(System.Text.Encoding.ASCII.GetString(str));
What I thought was an instant solution actually made me think a little harder about my initial problem. There must be a better way to get the username than by making an old Windows API call. Upon doing a little research I have come to the conclusion that the boys over at Microsoft did manage to include this into the .NET Framework rather seamlessly. I was rather shocked when I found out how very simple it was. You will only need the following:
//=======================================================
//Place this at the top, above your namespace declaration
using System.Security.Principal;
//=======================================================
//In a specific event, place the following.
string a;
a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
MessageBox.Show(a.ToString());
The System.Security.Principal.WindowsIdentity.GetCurrent() section opens us up to more than just the current user. When looking you will find the following additional methods/properties within this section of the .NET Framework:
AuthenticationType - Get's the type of authentication used to authenticate the user.Impersonates - Impersonates the user represented by the object.IsAnonymous - Indicates whether the user account is identified as an anonymous account by the system.IsAuthenticated - Determines if the user has been authenticated by Windows.IsGuest - Indicates whether the account is defined as a guest account by the system.IsSystem - Indicates whether the account is defined as a system account by the system..dll�s. Hope this helps someone, I found it to be rather interesting.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 20 May 2002 Editor: Chris Maunder |
Copyright 2002 by Nick Parker Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |