コード例 #1
0
        /// <summary>
        /// Adds a URL ACL exception via the HTTP API for the specified user account.
        /// </summary>
        /// <param name="url">A string containing the URL to add an exception for.</param>
        /// <param name="userAccount">A string containing the username of the user to add the exception for.</param>
        /// <param name="consoleOutput">An IConsoleOutputWrapper that provides a console for output. If NULL, then the default System.Console will be used.</param>
        /// <returns>A bool that indicates whether the exception was added successfully.</returns>
        public static bool AddUrlException(string url, string userAccount, IConsoleOutputWrapper consoleOutput = null)
        {
            bool ReturnValue = false;

            if (consoleOutput == null)
            {
                consoleOutput = new ConsoleOutput();
            }

            InitialiseHttpApi();

            if (CreateUrlAclSetFromUserAccount(url, userAccount, out HTTP_SERVICE_CONFIG_URLACL_SET UrlAclSet))
            {
                bool   ShouldRetry = true;
                uint   Result;
                int    RetryCount = 0;
                IntPtr UrlAclSetPointer;

                consoleOutput.WriteLine("Set configuration for '{0}'", UrlAclSet.KeyDesc.pUrlPrefix);
                consoleOutput.WriteLine("SDDL is '{0}'", UrlAclSet.ParamDesc.pStringSecurityDescriptor);
                consoleOutput.WriteLine();

                UrlAclSetPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET)));
                Marshal.StructureToPtr(UrlAclSet, UrlAclSetPointer, false);

                while (ShouldRetry && RetryCount++ < 4)
                {
                    ShouldRetry = false;

                    // Attempt to set the configuration and process the result
                    consoleOutput.Write("Attempting {0}: ", RetryCount);
                    Result = NativeMethods.HttpSetServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, UrlAclSetPointer, Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET)), IntPtr.Zero);
                    switch (Result)
                    {
                    case 0:
                        consoleOutput.WriteLine("Success.");
                        ReturnValue = true;
                        break;

                    case 183:     // Entry already exists
                        // Attempt to remove the existing configuration and retry
                        consoleOutput.WriteLine("Service configuration already exists. The existing configuration will be removed.");
                        RemoveUrlException(url);
                        ShouldRetry = true;
                        break;

                    default:
                        consoleOutput.WriteLine("Error {0} returned.", Result);
                        break;
                    }

                    consoleOutput.WriteLine();
                }
            }

            TerminateHttpApi();

            return(ReturnValue);
        }
コード例 #2
0
        /// <summary>
        /// Removes a URL ACL exception via the HTTP API.
        /// </summary>
        /// <param name="url">A string containing the URL to remove.</param>
        /// <param name="consoleOutput">An IConsoleOutputWrapper that provides a console for output. If NULL, then the default System.Console will be used.</param>
        /// <returns>A bool that indicates whether the exception was removed successfully.</returns>
        public static bool RemoveUrlException(string url, IConsoleOutputWrapper consoleOutput = null)
        {
            uint   Result;
            bool   ReturnValue = false;
            IntPtr UrlAclSetPointer;

            if (consoleOutput == null)
            {
                consoleOutput = new ConsoleOutput();
            }

            InitialiseHttpApi();

            HTTP_SERVICE_CONFIG_URLACL_SET UrlAclSet = new HTTP_SERVICE_CONFIG_URLACL_SET()
            {
                KeyDesc   = new HTTP_SERVICE_CONFIG_URLACL_KEY(url),
                ParamDesc = new HTTP_SERVICE_CONFIG_URLACL_PARAM("")
            };

            consoleOutput.WriteLine("Delete configuration for '{0}'", UrlAclSet.KeyDesc.pUrlPrefix);
            UrlAclSetPointer = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET)));
            Marshal.StructureToPtr(UrlAclSet, UrlAclSetPointer, false);

            // Attempt to set the configuration and process the result
            Result = NativeMethods.HttpDeleteServiceConfiguration(IntPtr.Zero, HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, UrlAclSetPointer, Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_URLACL_SET)), IntPtr.Zero);
            switch (Result)
            {
            case 0:
                consoleOutput.WriteLine("Success.");
                ReturnValue = true;
                break;

            default:
                consoleOutput.WriteLine("Error {0} returned.", Result);
                break;
            }

            TerminateHttpApi();

            consoleOutput.WriteLine();

            return(ReturnValue);
        }