EDN Admin
Well-known member
Hi,
I trying for a while to Create new session and run UI application , then send snapshots to client.
- I built WCF bridge hosted in IIS server (cloud)
- Win service that installed and run in user machine, it connected to WCF bridge, get username and password , and should create new session
- SessionUI app, that should run when session created, take snapshot and send to WCF bridge
- client application, connected to WCF bridge send user name and password and get snapshot
All work perfect if I manually logon to client windows, but after user reboot its windows, service connected to WCF bridge, create session, but SessionUI not run (I add it to startup).
My problem is:
- Why to create session do not perform and run the applications in the startup folder, do I need to add the sessionUI to "HKEY_CURRENT_USER...CurrentVersionRun" registry?, how to make it run?
- As I know I cant execute the sessionUI from inside service due to isolated session0, even though there are lots of "solutions" in the internet but non worked for me: for example: http://bit.ly/Xa8kfH , not work if boot the PC
Here what I tried, BTW, I make too many changes but still not work (it pass without error):
As I said ImpersonateValidUser passed with no errors!
What wrong, what I should change, what should do protected void ImpersonateValidUser(
string userName,
string domain,
string password)
{
if (string.IsNullOrEmpty(userName))
return;
IntPtr token = IntPtr.Zero;
try
{
if (LogonUser(
userName,
domain,
password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref token) != 0)
{
#region LoadUserProfile
// Load user profile
PROFILEINFO profileInfo = new PROFILEINFO();
profileInfo.dwSize = Marshal.SizeOf(profileInfo);
profileInfo.lpUserName = userName;
profileInfo.dwFlags = 1;
Boolean loadSuccess = LoadUserProfile(token, ref profileInfo);
if (!loadSuccess)
{
Console.WriteLine("LoadUserProfile()failed with error code: " + Marshal.GetLastWin32Error());
//throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (profileInfo.hProfile == IntPtr.Zero)
{
Console.WriteLine("LoadUserProfile() failed -HKCU handle was not loaded. Error code: " + Marshal.GetLastWin32Error());
//throw new Win32Exception(Marshal.GetLastWin32Error());
}
#endregion
}
if (ImpersonateLoggedOnUser(token) == 0)
{
#if ENABLELOGGING
ListenerConfig.logger.Warn("LoadUserProfile, ImpersonateLoggedOnUser failed ");
#endif
}
if (RevertToSelf())
{
if (LogonUser(
userName,
domain,
password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref token) != 0)
{
SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES();
sa.nLength = (uint)Marshal.SizeOf(sa);
if (DuplicateTokenEx(
token,
TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY,
ref sa,
(int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,
(int)TOKEN_TYPE.TokenPrimary,
ref m_AuthToken) == true)
{
m_NewWindowsIdentity = new WindowsIdentity(m_AuthToken);
m_ImpersonationContext = m_NewWindowsIdentity.Impersonate();
if (m_ImpersonationContext != null)
{
Console.WriteLine("After Impersonation succeeded:" + Environment.NewLine +
"User Name: " +
WindowsIdentity.GetCurrent
(TokenAccessLevels.MaximumAllowed).Name +
Environment.NewLine +
"SID: " + WindowsIdentity.GetCurrent(TokenAccessLevels.MaximumAllowed).User.Value);
//CloseHandle(token);
//CloseHandle(m_AuthToken);
// Do tasks after impersonating successfully
//AccessFileSystem();
//// Access HKCU after loading users profile
//AccessHkcuRegistry(profileInfo.hProfile);
// Unload user profile
// MSDN remarks
// http://msdn.microsoft.com/en-us/library/bb762282// (VS.85).aspx
// Before calling UnloadUserProfile you should ensure
// that all handles to keys that you have opened in the
// users registry hive are closed. If you do not close
// all open registry handles, the users profile fails
// to unload. For more information,
// see Registry Key Security
// and Access Rights and Registry Hives.
//UnloadUserProfile(m_AuthToken, profileInfo.hProfile);
// Undo impersonation
//m_ImpersonationContext.Undo();
}
}
else
{
int nErrorCode = Marshal.GetLastWin32Error();
System.Diagnostics.Debug.WriteLine("DuplicateToken() failed with error code: " + nErrorCode.ToString());
throw new Win32Exception(nErrorCode);
}
}
else
{
int nErrorCode = Marshal.GetLastWin32Error(); System.Diagnostics.Debug.WriteLine("LogonUser() failed with error code: " + nErrorCode.ToString());
throw new Win32Exception(nErrorCode);
}
}
else
{
int nErrorCode = Marshal.GetLastWin32Error(); System.Diagnostics.Debug.WriteLine("RevertToSelf() failed with error code: " + nErrorCode.ToString());
throw new Win32Exception(nErrorCode);
}
}
finally
{
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
}
}
View the full article