Ever thought of getting dynamic web reference from a WSDL? Then making dynamic proxies, and invoking the methods dynamically.

I had some requirement which includes all of the above topics. So after completing my test code I thought of publishing it here for educational purposes.

check it out if you need to do something similar or if you are stuck with some intermediate part:



There might be several possible secenarios where you might need to forcefully impersonate a user which is valid on some domain but without using identity impersonation.

Looking back in time of the Win32 apis, here is how it can be achieved:

The Win32 api called as LogonUser(string user, string domain, string password, LogonSessionType type, LogonProvider provider, out IntPtr token) is the question to our answer. This is a exported from advapi32.dll.

using System.Security;
using System.Security.Principal; 

[DllImport(“advapi32.dll”, SetLastError = true)]
static extern bool LogonUser( string user, string domain, string password, LogonSessionType logonType, LogonProvider logonProvider, out IntPtr token);

enum LogonSessionType : uint{Interactive = 2, Network, Batch, Service, NetworkCleartext = 8, NewCredentials}enum LogonProvider : uint{
Default = 0,
// default for platform (use this!)
WinNT35, // sends smoke signals to authority
WinNT40, // uses NTLM
WinNT50 // negotiates Kerb or NTLM

public void ImpersonateCurrentRequest(HttpContext ctxt)
IntPtr token = IntPtr.Zero;
bool result = LogonUser(“someusername”, “somedomain”, “somepassword”,LogonSessionType.Network,LogonProvider.Default,out token);
if (result)
WindowsIdentity iden = new WindowsIdentity(token);
WindowsPrincipal principal = new WindowsPrincipal(iden);
IPrincipal p = (IPrincipal)principal;
.User = p;

//end code

Note this has to be done before the request is processed, so it better to call this function from a httpmodule:BeginRequest()