I just tried WindowsIdentity.GetCurrent().Name - it does give sa. Why the
"David Thielen" wrote:
> Here you go. Same issue - list the Name as me, not sa (I use sa instead of
> administrator for our domain admin username).
>
> using System;
> using System.Runtime.InteropServices;
> using System.Security.Principal;
> using System.Threading;
>
> namespace TestImpersonate
> {
> class Program
> {
> [DllImport("advapi32.dll", SetLastError = true)]
> public static extern bool LogonUser(String lpszUsername, String
> lpszDomain, String lpszPassword,
> int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
> [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
> public extern static bool CloseHandle(IntPtr handle);
>
> const int LOGON32_PROVIDER_DEFAULT = 0;
> const int LOGON32_LOGON_NETWORK = 3;
>
> static void Main(string[] args)
> {
> string username = "sa";
> string password = "*****";
>
> AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
> string domainName = Thread.CurrentPrincipal.Identity.Name;
> domainName = domainName.Substring(0, domainName.IndexOf('\\'));
>
> IntPtr tokenHandle = new IntPtr(0);
> bool returnValue = LogonUser(username, domainName, password,
> LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
> ref tokenHandle);
> if (returnValue == false)
> throw new Exception("Windows logon Error: " +
> Marshal.GetLastWin32Error());
>
> WindowsIdentity identity = new WindowsIdentity(tokenHandle);
> WindowsImpersonationContext impersonationContext = identity.Impersonate();
>
> IPrincipal prin = Thread.CurrentPrincipal;
> Console.Out.WriteLine("user = " + prin.Identity.Name);
>
> impersonationContext.Undo();
> impersonationContext.Dispose();
> CloseHandle(tokenHandle);
>
> }
> }
> }
>
>
> --
> thanks - dave
> david_at_windward_dot_net
>
http://www.windwardreports.com >
> Cubicle Wars -
http://www.windwardreports.com/film.htm