Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I finally found a solution to my problem.</p> <p>Using this KB article <a href="http://support.microsoft.com/kb/306158" rel="nofollow">"How to implement impersonation in an ASP.NET application / Impersonate a Specific User in Code"</a> I found a way to impersonate my shared hosting <strong>FTP user</strong>.</p> <p>This way I would gain the privileges of the said user and not compromise the security of the server by lowering the security on the <em>NT AUTHORITY\NETWORK SERVICE</em> user.</p> <p>This is the code that I used:</p> <pre><code>using System; using System.Runtime.InteropServices; using System.Security.Principal; using System.Configuration; namespace App_Code { public class Impersonation : IDisposable { private WindowsImpersonationContext _impersonationContext; #region Win32 API Declarations private const int Logon32LogonInteractive = 2; //This parameter causes LogonUser to create a primary token. private const int Logon32ProviderDefault = 0; [DllImport("advapi32.dll")] private static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool RevertToSelf(); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern bool CloseHandle(IntPtr handle); #endregion public bool ImpersonateDefaultFtpUser() { return ImpersonateFtpUser(ConfigurationManager.AppSettings["ftpUsername"], ConfigurationManager.AppSettings["ftpDomain"], ConfigurationManager.AppSettings["ftpPassword"]); } public bool ImpersonateFtpUser(string userName, string domain, string password) { WindowsIdentity tempWindowsIdentity; var token = IntPtr.Zero; var tokenDuplicate = IntPtr.Zero; if (RevertToSelf()) { if (LogonUserA(userName, domain, password, Logon32LogonInteractive, Logon32ProviderDefault, ref token) != 0) { if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) { tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); _impersonationContext = tempWindowsIdentity.Impersonate(); if (_impersonationContext != null) { CloseHandle(token); CloseHandle(tokenDuplicate); return true; } } } } if (token != IntPtr.Zero) CloseHandle(token); if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate); return false; } public void UndoImpersonation() { if (_impersonationContext != null) _impersonationContext.Undo(); } /// &lt;summary&gt; /// Constructor. Impersonates the default ftp user. Impersonation lasts until /// the instance is disposed. /// &lt;/summary&gt; public Impersonation() { ImpersonateDefaultFtpUser(); } /// &lt;summary&gt; /// Constructor. Impersonates the requested user. Impersonation lasts until /// the instance is disposed. /// &lt;/summary&gt; public Impersonation(string userName, string domain, string password) { ImpersonateFtpUser(userName, domain, password); } #region IDisposable Pattern /// &lt;summary&gt; /// Revert to original user and cleanup. /// &lt;/summary&gt; protected virtual void Dispose(bool disposing) { if (!disposing) return; // Revert to original user identity UndoImpersonation(); if (_impersonationContext != null) _impersonationContext.Dispose(); } /// &lt;summary&gt; /// Explicit dispose. /// &lt;/summary&gt; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// &lt;summary&gt; /// Destructor /// &lt;/summary&gt; ~Impersonation() { Dispose(false); } #endregion } } </code></pre>
    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. 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