Exemplo n.º 1
0
        // 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);
        }
Exemplo n.º 2
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);
        }