예제 #1
0
 private bool LoadUserProfileWorker(int userSessionId)
 {
     // When a user logs on interactively, the system automatically loads the
     // user's profile. If a service or an application impersonates a user,
     // the system does not load the user's profile. Therefore, the service or
     // application should load the user's profile with LoadUserProfile.
     profileInfo            = new PROFILEINFO();
     profileInfo.dwSize     = Marshal.SizeOf(profileInfo);
     profileInfo.lpUserName = UserSessionHelper.GetUserSessionQueryInfo(userSessionId,
                                                                        WTS_INFO_CLASS.WTSUserName);
     return(LoadUserProfile(userTokenHandle, ref profileInfo));
 }
예제 #2
0
        public bool UserSessionDisconnect(string connectionName, string connectionId, int userSessionId)
        {
            EnsureValidClientConnection(connectionName, connectionId);

            UserSessionState userSessionState = new UserSessionState(userSessionId);
            UserSession      userSession      = new UserSession(UserSessionType.Disconnected);
            UserInfo         userInfo         = new UserInfo();

            if (UserSessionHelper.GetUserSessionUserInfo(userSessionId, out userInfo))
            {
                userSession.UserLoginInfo = userInfo;
            }
            else
            {
                // For machines without auto logon, password retrieval fails. We would like to
                // throw an exception here to fail the test case.
                throw new InvalidOperationException("Auto logon is required to be enabled for this task to succeed");
            }

            return(StateManager.Current.Transition(connectionName, userSessionState, userSession, UserSessionAction.Disconnect));
        }
예제 #3
0
            internal ImpersonateUser()
            {
                string eventLogContent = "";

                // Log current user identity
                eventLogContent += "Identity before impersonation: [" +
                                   WindowsIdentity.GetCurrent().Name + "]" + Environment.NewLine;

                // get the user token to impersonate.
                int userSessionId = UserSessionHelper.GetUserConsoleSession();

                if (!WTSQueryUserToken((uint)userSessionId, out userTokenHandle))
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new Win32Exception("WTSQueryUserToken failed with error code: [" + errorCode + "]" +
                                             Environment.NewLine + "This will also fail when test is run over TS");
                }

                // do the actual impersonation
                if (!ImpersonateLoggedOnUser(userTokenHandle))
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    throw new Win32Exception("ImpersonateLoggedOnUser failed with error code: [" + errorCode + "]");
                }

                // Log impersonated user identity
                eventLogContent += "Identity after impersonation: [" +
                                   WindowsIdentity.GetCurrent().Name + "] " + Environment.NewLine +
                                   "Session Id used for impersonation: [" +
                                   userSessionId + "]" + Environment.NewLine;

                // Since we are impersonating logged on user,
                // we skip the LoadUserProfile step.
                //



                // Services that change impersonation should call RegDisablePredefinedCache
                // before using any of the predefined handles
                int isPredinedCacheDisabled;

                if (Environment.OSVersion.Version.Major < 6)
                {
                    isPredinedCacheDisabled = RegDisablePredefinedCache();
                }
                else
                {
                    // This API is introduced only from Vista.
                    isPredinedCacheDisabled = RegDisablePredefinedCacheEx();
                }
                // non-zero values indicates fail
                if (isPredinedCacheDisabled != 0)
                {
                    int errorCode = Marshal.GetLastWin32Error();
                    eventLogContent += "RegDisablePredefinedCache returned non-zero value. ErrorCode: [" +
                                       errorCode + "]" + Environment.NewLine;
                }

                // log the events into event log
                EventLog eventLog = new EventLog();

                eventLog.Source = serviceName;
                eventLog.WriteEntry(eventLogContent, EventLogEntryType.Information, eventId); // random event Id
                eventLog.Close();
            }
예제 #4
0
 public int UserSessionGetConsoleSessionId()
 {
     return(UserSessionHelper.GetUserConsoleSession());
 }
예제 #5
0
 public bool UserSessionLoginToConsole(string username, string domain, string password)
 {
     return(UserSessionHelper.TryUserSessionLogonToConsole(username, domain, password));
 }
예제 #6
0
 public bool UserSessionUnlockConsole()
 {
     return(UserSessionHelper.TryUserSessionActivateConsole(UserSessionHelper.GetUserConsoleSession()));
 }