Beispiel #1
0
        private Impersonation(string username, string domain, string password, LogonType logonType, BuiltinUser builtinUser)
        {
            switch (builtinUser)
            {
            case BuiltinUser.None:
                if (string.IsNullOrEmpty(username))
                {
                    return;
                }
                break;

            case BuiltinUser.LocalService:
                username = "******";
                break;

            case BuiltinUser.NetworkService:
                username = "******";
                break;
            }

            IntPtr userToken            = IntPtr.Zero;
            IntPtr userTokenDuplication = IntPtr.Zero;

            // Logon with user and get token.
            bool loggedOn = NativeMethod.LogonUser(username, domain, password, logonType, LogonProvider.Default, out userToken);

            if (loggedOn)
            {
                try
                {
                    // Create a duplication of the usertoken, this is a solution
                    // for the known bug that is published under KB article Q319615.
                    if (NativeMethod.DuplicateToken(userToken, 2, ref userTokenDuplication))
                    {
                        // Create windows identity from the token and impersonate the user.
                        WindowsIdentity identity = new WindowsIdentity(userTokenDuplication);
                        _impersonationContext = identity.Impersonate();
                    }
                    //else
                    //{
                    //    // Token duplication failed!
                    //    // Use the default ctor overload
                    //    // that will use Mashal.GetLastWin32Error();
                    //    // to create the exceptions details.
                    //    throw new Win32Exception();
                    //}
                }
                finally
                {
                    // Close usertoken handle duplication when created.
                    if (!userTokenDuplication.Equals(IntPtr.Zero))
                    {
                        // Closes the handle of the user.
                        NativeMethod.CloseHandle(userTokenDuplication);
                        userTokenDuplication = IntPtr.Zero;
                    }

                    // Close usertoken handle when created.
                    if (!userToken.Equals(IntPtr.Zero))
                    {
                        // Closes the handle of the user.
                        NativeMethod.CloseHandle(userToken);
                        userToken = IntPtr.Zero;
                    }
                }
            }
            //else
            //{
            //    // Logon failed!
            //    // Use the default ctor overload that
            //    // will use Mashal.GetLastWin32Error();
            //    // to create the exceptions details.
            //    throw new Win32Exception();
            //}
        }
Beispiel #2
0
    private Impersonation(String username, String domain, String password, LogonType logonType, BuiltinUser builtinUser)
    {
        switch (builtinUser)
            {
                case BuiltinUser.None: if (String.IsNullOrEmpty(username)) return; break;
                case BuiltinUser.LocalService: username = "******"; break;
                case BuiltinUser.NetworkService: username = "******"; break;
            }

            IntPtr userToken = IntPtr.Zero;
            IntPtr userTokenDuplication = IntPtr.Zero;

            // Logon with user and get token.
            bool loggedOn = LogonUser(username, domain, password,
                logonType, LogonProvider.Default,
                out userToken);

            if (loggedOn)
            {
                try
                {
                    // Create a duplication of the usertoken, this is a solution
                    // for the known bug that is published under KB article Q319615.
                    if (DuplicateToken(userToken, 2, ref userTokenDuplication))
                    {
                        // Create windows identity from the token and impersonate the user.
                        WindowsIdentity identity = new WindowsIdentity(userTokenDuplication);
                        _impersonationContext = identity.Impersonate();
                    }
                    else
                    {
                        // Token duplication failed!
                        // Use the default ctor overload
                        // that will use Mashal.GetLastWin32Error();
                        // to create the exceptions details.
                        throw new Win32Exception();
                    }
                }
                finally
                {
                    // Close usertoken handle duplication when created.
                    if (!userTokenDuplication.Equals(IntPtr.Zero))
                    {
                        // Closes the handle of the user.
                        CloseHandle(userTokenDuplication);
                        userTokenDuplication = IntPtr.Zero;
                    }

                    // Close usertoken handle when created.
                    if (!userToken.Equals(IntPtr.Zero))
                    {
                        // Closes the handle of the user.
                        CloseHandle(userToken);
                        userToken = IntPtr.Zero;
                    }
                }
            }
            else
            {
                // Logon failed!
                // Use the default ctor overload that
                // will use Mashal.GetLastWin32Error();
                // to create the exceptions details.
                throw new Win32Exception();
            }
    }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Impersonation"/> class and
 /// impersonates as a built in service account.
 /// </summary>
 /// <param name="builtinUser">The built in user to impersonate - either
 /// Local Service or Network Service. These users can only be impersonated
 /// by code running as System.</param>
 public Impersonation(BuiltinUser builtinUser)
     : this(string.Empty, "NT AUTHORITY", string.Empty, LogonType.Service, builtinUser)
 {
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Impersonation"/> class and
 /// impersonates as a built in service account.
 /// </summary>
 /// <param name="builtinUser">The built in user to impersonate - either
 /// Local Service or Network Service. These users can only be impersonated
 /// by code running as System.</param>
 public Impersonation(BuiltinUser builtinUser)
     : this(String.Empty, "NT AUTHORITY", String.Empty, LogonType.Service, builtinUser)
 {
 }