コード例 #1
0
ファイル: User.cs プロジェクト: maxrogoz/COSystemArchitect
 public User(IUserDef def)
     : this()
 {
     Login = def.Login;
     Domain = def.Domain;
     Password = def.Password;
 }
コード例 #2
0
ファイル: UserDef.cs プロジェクト: maxrogoz/COSystemArchitect
 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);
         });
     });
 }
コード例 #4
0
        /// <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();
                }
            }
        }
コード例 #5
0
        /// <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());
                }
            }
        }