I've had similar problems but connecting to OLAP.
I appreciate its a bit heavy, it took me a while to get working, but here's the code.
It believe you have to enable unsafe code in your project for it to work.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Management;
public class Impersonate : IDisposable
{
private const int SERVICE_ALL_ACCESS = 0xF01FF;
private const int SC_MANAGER_ALL_ACCESS = 0xF003F;
private const int SERVICE_CONFIG_DESCRIPTION = 0x1;
private const int SERVICE_CONFIG_FAILURE_ACTIONS = 0x2;
private const int SERVICE_NO_CHANGE = -1;
private const int ERROR_ACCESS_DENIED = 5;
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_LOGON_NETWORK = 3;
public const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
public const int LOGON32_PROVIDER_DEFAULT = 0;
public const int LOGON32_PROVIDER_WINNT50 = 3;
public const int LOGON32_PROVIDER_WINNT40 = 2;
public const int LOGON32_PROVIDER_WINNT35 = 1;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SERVICE_FAILURE_ACTIONS
{
public int dwResetPeriod;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpRebootMsg;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpCommand;
public int cActions;
public IntPtr lpsaActions;
}
[StructLayout(LayoutKind.Sequential)]
public class SC_ACTION
{
public Int32 type;
public UInt32 dwDelay;
}
[DllImport("advapi32.dll")]
public static extern IntPtr OpenSCManager(string lpMachineName, string lpDatabaseName, int dwDesiredAccess);
[DllImport("advapi32.dll")]
public static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, int dwDesiredAccess);
[DllImport("advapi32.dll", EntryPoint = "ChangeServiceConfig2")]
public static extern bool ChangeServiceFailureActions(IntPtr hService, int dwInfoLevel,
[MarshalAs(UnmanagedType.Struct)]
ref SERVICE_FAILURE_ACTIONS lpInfo);
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "QueryServiceConfig2W")]
public static extern Boolean QueryServiceConfig2(IntPtr hService, UInt32 dwInfoLevel, IntPtr buffer, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);
[DllImport("kernel32.dll")]
public static extern int GetLastError();
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr* arguments);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr existingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr duplicateTokenHandle);
IntPtr token = IntPtr.Zero;
IntPtr dupToken = IntPtr.Zero;
WindowsImpersonationContext impersonatedUser = null;
public Impersonate(string username, string computerName, string password)
{
try
{
bool isSuccess = LogonUser(username.Replace(@".\", ""), computerName, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token);
if (!isSuccess)
throw new Exception("Error impersonating");
isSuccess = DuplicateToken(token, 2, ref dupToken);
if (!isSuccess)
{
throw new Exception("Error impersonating");
}
WindowsIdentity newIdentity = new WindowsIdentity(dupToken);
impersonatedUser = newIdentity.Impersonate();
}
catch (Exception ex)
{
throw ex;
}
}
~Impersonate()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (impersonatedUser != null)
impersonatedUser.Undo();
CloseHandle(token);
}
}
This is how to use it:
Pass in the username, domain and password.
using (new Impersonate.Impersonate("Adam", "Eden", "Pa$$w0rd"))
{
}