/// <summary> /// Executes the specified method under the specified credentials. /// </summary> /// <param name="methodToRunAs"></param> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="domain"></param> public static void RunAs(RunAsDelegate methodToRunAs, string userName, string password, string domain) { IntPtr token = IntPtr.Zero; IntPtr hTokenDuplicate = IntPtr.Zero; try { if (RevertToSelf() != 0) { if (LogonUser( userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out token)) { if (DuplicateToken(token, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref hTokenDuplicate) != 0) { WindowsIdentity tempWindowsIdentity = new WindowsIdentity(hTokenDuplicate); using (WindowsImpersonationContext impersonationContext = tempWindowsIdentity.Impersonate()) { methodToRunAs(); impersonationContext.Undo(); } } else { throw new ImpersonationException(userName); } } else { throw new ImpersonationException(userName); } } else { throw new ImpersonationException(userName); } } finally { if (token != IntPtr.Zero) { CloseHandle(token); } if (hTokenDuplicate != IntPtr.Zero) { CloseHandle(hTokenDuplicate); } } }
// ReSharper disable MethodOverloadWithOptionalParameter /// <summary> /// Executes the specified method with the specified parameters and returns the result using the specified SharePoint 2010 Secure Store Provider Type Name and SharePoint 2010 Secure Store Application Id. /// </summary> /// <param name="providerTypeName"></param> /// <param name="applicationId"></param> /// <param name="methodToRunAs"></param> /// <param name="parameters"></param> /// <returns></returns> public static object RunAs(string providerTypeName, string applicationId, RunAsDelegate methodToRunAs, params object[] parameters) { object result; using (ImpersonationHelper impersonationContext = new ImpersonationHelper(providerTypeName, applicationId)) { impersonationContext.ImpersonateUser(); try { result = methodToRunAs.DynamicInvoke(parameters); } catch (TargetInvocationException ex) { throw ex.InnerException; } } return(result); }
public static void RunAs(RunAsDelegate MethodToRunAs, string Username, string Password) { string userName; string domain; if (Username.IndexOf('\\') > 0) { //a domain name was supplied string[] usernameArray = Username.Split('\\'); userName = usernameArray[1]; domain = usernameArray[0]; } else { //there was no domain name supplied userName = Username; domain = "."; } RunAs(MethodToRunAs, userName, Password, domain); }
public static void RunAs(RunAsDelegate MethodToRunAs, string Username, string Password, string Domain) { IntPtr imp_token; WindowsIdentity wid_admin = null; WindowsImpersonationContext wic = null; try { if (LogonUser(Username, string.IsNullOrEmpty(Domain) ? "." : Domain, Password, 9, 0, out imp_token)) { //the impersonation suceeded wid_admin = new WindowsIdentity(imp_token); wic = wid_admin.Impersonate(); //run the delegate method MethodToRunAs(); } else { throw new Exception(string.Format("Could not impersonate user {0} in domain {1} with the specified password.", Username, Domain)); } } catch (Exception se) { int ret = Marshal.GetLastWin32Error(); if (wic != null) { wic.Undo(); } throw new Exception("Error code: " + ret.ToString(), se); } finally { //revert to self if (wic != null) { wic.Undo(); } } }
/// <summary> /// Executes the specified method using the specified SharePoint 2010 Secure Store provider type name and SharePoint 2010 Secure Store Application Id. /// </summary> /// <param name="providerTypeName"></param> /// <param name="applicationId"></param> /// <param name="methodToRunAs"></param> public static void RunAs(string providerTypeName, string applicationId, RunAsDelegate methodToRunAs) { RunAs(providerTypeName, applicationId, methodToRunAs, new object[] { }); }