Пример #1
0
 public User(IUserDef def)
     : this()
 {
     Login = def.Login;
     Domain = def.Domain;
     Password = def.Password;
 }
Пример #2
0
 public UserDef(IUserDef def)
 {
     if (def == null)
         return;
     Login = def.Login;
     Domain = def.Domain;
     Password = def.Password;
 }
Пример #3
0
 /// <summary>
 /// Copies all files from the source path to the destination one
 /// uses impersonation with configured credentials for source and destination paths
 /// </summary>
 /// <param name="SourceFolder">Source path</param>
 /// <param name="sourceUser">User for access to the source path</param>
 /// <param name="DestFolder">Destination path</param>
 /// <param name="destUser">User for access to the destination path</param>
 public void CopyPathWithUserRights(string SourceFolder, IUserDef sourceUser, string DestFolder, IUserDef destUser)
 {
     RemoteImpersonator.ImpersonateRemoteUser(destUser, DestFolder, delegate() {
         LocalImpersonator.ImpersonateLocalUser(sourceUser, delegate()
         {
             CopyPath(SourceFolder, DestFolder);
         });
     });
 }
        /// <summary>
        /// Impersonate the user and fire delegate with user rights
        /// If user is null then impersonalization will not be done
        /// </summary>
        /// <param name="user">The user whose permissions are used to impersonalization</param>
        /// <param name="user">Delegate, executed in impersonate mode</param>
        public static void ImpersonateLocalUser(IUserDef user, Action impersonateDelegate)
        {
            if (user == null || user.Login == null)
            {
                impersonateDelegate();
                return;
            }

            SafeTokenHandle safeTokenHandle;
            string userName = user.Login;
            string domainName = user.Domain;
            string password = user.Password;

            const int LOGON32_PROVIDER_DEFAULT = 0;
            //This parameter causes LogonUser to create a primary token.
            const int LOGON32_LOGON_INTERACTIVE = 2;

            // Call LogonUser to obtain a handle to an access token.
            bool returnValue = LogonUser(userName, domainName, password,
                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                out safeTokenHandle);

            if (false == returnValue)
            {
                int ret = Marshal.GetLastWin32Error();
                throw new UnauthorizedAccessException(String.Format("LogonUser failed with error code : {0}", ret));
            }
            using (safeTokenHandle)
            {
                // Use the token handle returned by LogonUser.
                WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
                using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
                {
                    impersonateDelegate();
                }
            }
        }
        /// <summary>
        /// Impersonate the user and fire delegate with user rights
        /// If user is null then impersonalization will not be done
        /// </summary>
        /// <param name="user">The user whose permissions are used to impersonalization</param>
        /// <param name="user">Delegate, executed in impersonate mode</param>
        public static void ImpersonateRemoteUser(IUserDef user, string remotePath, Action impersonateDelegate)
        {
            if (user == null || user.Login == null)
            {
                impersonateDelegate();
                return;
            }

            Uri uri = new Uri(remotePath);
            if (!uri.IsUnc)
                throw new ArgumentException("ImpersonateRemoteUser: remotePath is not UNC (" + remotePath + ")");

            using (RemoteImpersonator unc = new RemoteImpersonator())
            {
                if (unc.NetUseWithCredentials(remotePath, user.Login, user.Domain, user.Password))
                {
                    impersonateDelegate();
                }
                else
                {
                    throw new InvalidOperationException("Failed to connect to " + remotePath + "\r\nLastError = " + unc.LastError.ToString());
                }
            }
        }