/// <summary> /// Deletes the current <see cref="HttpUrlReservation"/>. /// </summary> /// <exception cref="HttpUrlReservationNotFoundException">If the current <see cref="HttpUrlReservation"/> does not still exist.</exception> /// <exception cref="HttpServerException">If an internal error has been occured (use the <see cref="Win32Exception.NativeErrorCode"/> for more information).</exception> public void Delete() { HttpServer.Initialize(); try { var configInformation = new HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET(this.url, null); var result = HttpServerApi.HttpDeleteServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero); if (result == Win32Error.ERROR_FILE_NOT_FOUND) { throw new HttpUrlReservationNotFoundException(result, string.Format(CultureInfo.CurrentUICulture, HttpServerResources.UrlReservationToDeleteNotFound, this.url)); } if (result != Win32Error.NO_ERROR) { throw new HttpServerException(result); } } finally { HttpServer.Terminate(); } }
/// <summary> /// Update the <see cref="HttpUrlReservationAccessRule"/> of the current <see cref="HttpUrlReservation"/>. /// </summary> /// <exception cref="HttpUrlReservationNotFoundException">If the current <see cref="HttpUrlReservation"/> does not still exist.</exception> /// <exception cref="HttpServerException">If an internal error has been occured (use the <see cref="Win32Exception.NativeErrorCode"/> for more information).</exception> private void Update() { HttpServer.Initialize(); try { var ssdl = this.GetSecurityDescriptorSddlForm(AccessControlSections.Access); var configInformation = new HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET(this.url, ssdl); // Delete the URL reservation record var result = HttpServerApi.HttpDeleteServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero); if (result == Win32Error.ERROR_FILE_NOT_FOUND) { throw new HttpUrlReservationNotFoundException(result, string.Format(CultureInfo.CurrentUICulture, HttpServerResources.UrlReservationToUpdateNotFound, this.url)); } // Add the URL reservation record with the new SDDL. result = HttpServerApi.HttpSetServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero); if (result != Win32Error.NO_ERROR) { throw new HttpServerException(result); } } finally { HttpServer.Terminate(); } }
/// <summary> /// Register an URL reservation in the Windows HTTP Server. /// </summary> /// <param name="url">URL to register.</param> /// <param name="rule">Rule to apply to the URL to register.</param> /// <returns>An instance of <see cref="HttpUrlReservation"/> which allows to manage the URL reservation.</returns> public static HttpUrlReservation RegisterUrlReservation(string url, HttpUrlReservationAccessRule rule) { Contracts.IsNotNull(url, nameof(url)); Contracts.IsNotNull(rule, nameof(rule)); Initialize(); try { // Create the ACL access var acl = new DiscretionaryAcl(false, false, 1); acl.AddAccess(rule.AccessControlType, (SecurityIdentifier)rule.IdentityReference, (int)rule.HttpUrlReservationAccessRights, rule.InheritanceFlags, rule.PropagationFlags); // Create the security descriptor and convert it to SDDL. var securityDescriptor = new CommonSecurityDescriptor(false, false, ControlFlags.DiscretionaryAclPresent, (SecurityIdentifier)rule.IdentityReference, null, null, acl); var ssdl = securityDescriptor.GetSddlForm(AccessControlSections.Access); var configInformation = new HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET(url, ssdl); var result = HttpServerApi.HttpSetServiceConfiguration(IntPtr.Zero, HttpServerApi.HTTP_SERVICE_CONFIG_ID.HttpServiceConfigUrlAclInfo, configInformation, Marshal.SizeOf(configInformation), IntPtr.Zero); if (result == Win32Error.ERROR_ALREADY_EXISTS) { throw new HttpUrlReservationAlreadyExistsException(result, string.Format(CultureInfo.CurrentUICulture, HttpServerResources.UrlReservationAlreadyExists, url)); } if (result != Win32Error.NO_ERROR) { throw new HttpServerException(result); } return(new HttpUrlReservation(url, new[] { rule })); } finally { Terminate(); } }
/// <summary> /// Converts the specified <paramref name="urlAcl"/> structure to the <see cref="HttpUrlReservation"/> instance. /// </summary> /// <param name="urlAcl"><see cref="HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET"/> to convert into <see cref="HttpUrlReservation"/>.</param> /// <returns>The <see cref="HttpUrlReservation"/> converted.</returns> private static HttpUrlReservation ConvertUrlAclToUrlReservation(HttpServerApi.HTTP_SERVICE_CONFIG_URLACL_SET urlAcl) { var accessRules = new List <HttpUrlReservationAccessRule>(); // Parse the security descriptor var securityDescriptor = new CommonSecurityDescriptor(false, false, urlAcl.ParamDesc.StringSecurityDescriptor); // For each ACE rule create a HttpUrlReservationAccessRule foreach (CommonAce rule in securityDescriptor.DiscretionaryAcl) { // Translate the security identifier to real login name (instead of the SID) var id = (IdentityReference)rule.SecurityIdentifier; if (id.IsValidTargetType(typeof(NTAccount))) { id = id.Translate(typeof(NTAccount)); } accessRules.Add(new HttpUrlReservationAccessRule(id, (HttpUrlReservationAccessRights)rule.AccessMask)); } return(new HttpUrlReservation(urlAcl.KeyDesc.UrlPrefix, accessRules)); }