I also use Impersonation if needed:
using System.Security.Principal;
using System.Runtime.InteropServices;
#region Constants
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
#endregion
public WindowsImpersonationContext impersonationContext;
#region Win32 API
[DllImport("advapi32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public 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, ExactSpelling = true)]
public static extern int DuplicateToken(IntPtr ExistingTokenHandle, int ImpersonationLevel, ref IntPtr DuplicateTokenHandle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern long CloseHandle(IntPtr handle);
#endregion
public bool Impersonate(string userName, string domain, string password)
{
try
{
bool functionReturnValue = false;
WindowsIdentity tempWindowsIdentity = default(WindowsIdentity);
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
functionReturnValue = false;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if ((impersonationContext != null))
{
functionReturnValue = true;
}
}
}
}
if (!tokenDuplicate.Equals(IntPtr.Zero))
{
CloseHandle(tokenDuplicate);
}
if (!token.Equals(IntPtr.Zero))
{
CloseHandle(token);
}
return functionReturnValue;
}
catch (Exception ex)
{
SQMSLog.WriteLogEntry(ex.Message, "SYSTEM");
return false;
}
}
public void UndoImpersonate()
{
impersonationContext.Undo();
}
Call the Impersonate Function First then:
//Add Miscellaneous Data Details
data.ListNameEx = Settings.Default.SPListLoader_List_Name;
//Instantiate the Web Service
SPListService.Lists lists = new SPListLoaderService.SPListService.Lists();
//Bind Appropriate Credentials to the Web Service
lists.Credentials = new NetworkCredential(data.UserName, data.Password, data.Domain);
//Set the List Object URI
lists.Url = data.URI.TrimEnd('/') + "/_vti_bin/lists.asmx";
Once complete YOU NEED TO RELEASE THE CONTEXT:
UndoImpersonate();
The above is very important, what ever you execute once you have called Impersonate(); will run under that users credentials.
Hope this Helps.
Cheers.