Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you debug do you see any error in</p> <pre><code>bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle); </code></pre> <p>or in</p> <pre><code>bool bRetVal = DuplicateToken(pExistingTokenHandle, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref pDuplicateTokenHandle); </code></pre> <p>?</p> <p>Inside your button click event, try add a <code>Using</code> statement:</p> <pre><code>private void btnOpen_Click(object sender, EventArgs e) { try { using (newUser = cls.ImpersonateUser("username", "domain", "password")) { string fileName = @"\\network_computer\Test\Test.doc"; System.Diagnostics.Process.Start(fileName); } } catch (Exception ex) { throw ex; } finally { if (newUser != null) newUser.Undo(); } } </code></pre> <p><strong>ANSWER UPDATE (#01):</strong></p> <p>Try the following code:</p> <pre><code>public class clsImpersonate { #region 'Impersonation' // obtains user token [DllImport("advapi32.dll", SetLastError = true)] public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); // closes open handes returned by LogonUser [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public extern static bool CloseHandle(IntPtr handle); public IntPtr ImpersonateUser(string sUsername, string sDomain, string sPassword) { // initialize token IntPtr pExistingTokenHandle = new IntPtr(0); // if domain name was blank, assume local machine if (sDomain == "") sDomain = System.Environment.MachineName; try { string sResult = null; const int LOGON32_PROVIDER_DEFAULT = 0; // create token const int LOGON32_LOGON_INTERACTIVE = 2; // get handle to token bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle); // did impersonation fail? if (false == bImpersonated) { int nErrorCode = Marshal.GetLastWin32Error(); sResult = "LogonUser() failed with error code: " + nErrorCode + "\r\n"; // show the reason why LogonUser failed //MessageBox.Show(sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Get identity before impersonation sResult += "Before impersonation: " + WindowsIdentity.GetCurrent().Name + "\r\n"; return pExistingTokenHandle; } catch (Exception ex) { throw ex; } } public bool FreeImpersonationResource(IntPtr _token) { // close handle(s) if (_token != IntPtr.Zero) return CloseHandle(_token); else return true; } #endregion } </code></pre> <p>And then, the button click event:</p> <pre><code>private void btnOpen_Click(object sender, EventArgs e) { try { IntPtr token = cls.ImpersonateUser("username", "domain", "password"); using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(token)) { string fileName = @"\\network_computer\Test\Test.doc"; System.Diagnostics.Process.Start(fileName); } cls.FreeImpersonationResource(token); } catch (Exception ex) { throw ex; } } </code></pre> <p>Note:<br> - If you use <code>LOGON32_LOGON_INTERACTIVE</code> inside LogonUser function, you don't need to duplicate the token, because the handle you get is a Primary Token that you can immediately use. (Take a look at <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa378184%28v=vs.85%29.aspx" rel="nofollow">this MSDN page</a>).<br> - If you use a <code>using</code> statement for your impersonation context you don't need to <code>Undo</code>, because it "automatically undo" at the end of the <code>using</code>.<br> - If it still doesn't work, try using the IP Address of the server instead of its domain name.<br> - If it still doesn't work, the last thing I can suggest is to take your original code and move the entire method <code>ImpersonateUser</code> inside the button click event, and then execute the original instruction <code>System.Diagnostics.Process.Start(fileName);</code> immediately after the instruction <code>System.Diagnostics.Process.Start(fileName);</code>. </p> <p>I hope at least some part of this answer will help :)</p> <p><strong>UPDATE (#02)</strong>:</p> <p>I've tried this myself and it's working, using the code in my previous edit with the following modification:</p> <ul> <li><p>inside the class <code>clsImpersonate</code> you have to change the type for the <code>LogonUser</code> function to <code>LOGON32_LOGON_NEW_CREDENTIALS</code>:</p> <pre><code>// create token const int LOGON32_LOGON_INTERACTIVE = 2; const int LOGON32_LOGON_NEW_CREDENTIALS = 9; // get handle to token bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle); </code></pre></li> <li><p>inside the button click event you have to change the way you use Process.Start(). Also check <a href="http://www.dotnetperls.com/process-start" rel="nofollow">this link</a> for some sample on Process.Start functionality</p> <pre><code>private void button1_Click(object sender, EventArgs e) { try { IntPtr token = cls.ImpersonateUser("username", "domain", "password"); using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(token)) { string fileName = @"Winword.exe"; // the name of the sw you will use to open your file, I suppose MS Word string argument = @"\\network_computer\Test\Test.doc"; // the path pf the file you want to open Process process = new Process(); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = fileName; info.Arguments = argument; process.StartInfo = info; process.Start(); } cls.FreeImpersonationResource(token); } catch (Exception ex) { throw ex; } } </code></pre></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload