Exemplo n.º 1
0
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if (!_disposed)
            {
                // If disposing equals true, dispose all managed
                // and unmanaged resources.
                if (disposing)
                {
                    if (_impersonationContext != null)
                    {
                        _impersonationContext.Undo();
                        _impersonationContext.Dispose();
                    }

                    if (_tokenDuplicate != IntPtr.Zero)
                    {
                        if (!ImpersinatorNativeMethods.CloseHandle(_tokenDuplicate))
                        {
                            // Uncomment if you need to know this case.
                            ////throw new Win32Exception();
                        }
                    }

                    if (_token != IntPtr.Zero)
                    {
                        if (!ImpersinatorNativeMethods.CloseHandle(_token))
                        {
                            // Uncomment if you need to know this case.
                            ////throw new Win32Exception();
                        }
                    }
                }

                _disposed = true;
            }
        }
Exemplo n.º 2
0
        private void Initialize()
        {
            if (!ImpersinatorNativeMethods.LogonUser(
                    _username,
                    _domain,
                    _password,
                    ImpersinatorNativeMethods.LogonType.NewCredentials,
                    ImpersinatorNativeMethods.LogonProvider.Default,
                    out _token))
            {
                throw new Win32Exception();
            }

            if (!ImpersinatorNativeMethods.DuplicateToken(
                    _token,
                    ImpersinatorNativeMethods.SecurityImpersonationLevel.Impersonation,
                    out _tokenDuplicate))
            {
                throw new Win32Exception();
            }

            _impersonationContext = new WindowsIdentity(_tokenDuplicate).Impersonate();
            // Do stuff with your share here.
        }