Exemple #1
0
        // 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);
        }