/// <summary> /// Returns user credentials from the secure store. /// </summary> /// <param name="providerTypeName"></param> /// <param name="applicationId"></param> /// <returns></returns> public static NetworkCredential ReadCredentialsFromSecureStore(string providerTypeName, string applicationId) { using (ImpersonationHelper impersonationContext = new ImpersonationHelper(providerTypeName, applicationId)) { var userCredentials = impersonationContext.ReadUserCredentialsFromSecureStore(providerTypeName, applicationId); return(new NetworkCredential(GetString(userCredentials.UserName), GetString(userCredentials.Password))); } }
/// <summary> /// Returns an ICredentials object that represents the credentials stored in the secure store. /// </summary> /// <param name="providerTypeName"></param> /// <param name="applicationId"></param> /// <returns></returns> public static ICredentials GetCredentialsFromSecureStore(string providerTypeName, string applicationId) { ICredentials credentials; using (var impersonationContext = new ImpersonationHelper(providerTypeName, applicationId)) { credentials = impersonationContext.GetNetworkCredentials(); } return(credentials); }
// ReSharper disable MethodOverloadWithOptionalParameter public static object InvokeAsUser(string providerTypeName, string applicationId, Delegate methodToCall, params object[] parameters) { object result; using (var impersonationContext = new ImpersonationHelper(providerTypeName, applicationId)) { impersonationContext.ImpersonateUser(); result = methodToCall.DynamicInvoke(parameters); } return(result); }
// 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); }