public static void EnableCors(this IPipelines pipelines, CorsConfiguration corsConfiguration) { pipelines.AfterRequest.AddItemToEndOfPipeline(ctx => { UpdateResponseHeaders(ctx.Request, ctx.Response, corsConfiguration); }); pipelines.OnError.AddItemToEndOfPipeline((ctx, exception) => { if (exception == null) { // Nothing to serialize, just return default 500 response return HttpStatusCode.InternalServerError; } Response response; if (ctx.Request.Headers.Accept.Any(x => x.Item1.ToLowerInvariant().Contains("application/json"))) { // Return the exception detail as JSON response = new JsonResponse(new ExceptionDetailObject(exception), new DefaultJsonSerializer()) {StatusCode = HttpStatusCode.InternalServerError}; } else { // Return the exception message as text/plain response = new TextResponse(HttpStatusCode.InternalServerError, exception.Message); } UpdateResponseHeaders(ctx.Request, response, corsConfiguration); return response; }); }
private static void UpdateResponseHeaders(Request request, Response response, CorsConfiguration corsConfiguration) { if (!request.Headers.Keys.Contains("Origin")) return; response.WithHeader("Access-Control-Allow-Origin", corsConfiguration.AllowOrigin); if (request.Method.Equals("OPTIONS")) { response .WithHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS") .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"); } }
/// <summary> /// Create a new bootstrapper that initializes itself from the brightstarService section /// of the application (or web) configuration file. /// </summary> /// <exception cref="ConfigurationErrorsException">Raised if the brightstarService configuration /// section does not exist in the application configuration file, or if the configuration is /// invalid.</exception> public BrightstarBootstrapper() { var configuration = ConfigurationManager.GetSection("brightstarService") as BrightstarServiceConfiguration; if (configuration == null) throw new ConfigurationErrorsException(Strings.NoServiceConfiguration); _brightstarService = BrightstarService.GetClient(configuration.ConnectionString); _storePermissionsProvider = configuration.StorePermissionsProvider ?? new FallbackStorePermissionsProvider(StorePermissions.All); _systemPermissionsProvider = configuration.SystemPermissionsProvider ?? new FallbackSystemPermissionsProvider(SystemPermissions.All); _authenticationProviders = configuration.AuthenticationProviders ?? new Collection<IAuthenticationProvider> {new NullAuthenticationProvider()}; _corsConfiguration = configuration.CorsConfiguration; }
private static CorsConfiguration ProcessCorsOptions(XmlElement cors) { var configuration = new CorsConfiguration(); if (cors == null) return configuration; bool disableCors; if (cors.HasAttribute("disabled") && bool.TryParse(cors.GetAttribute("disabled"), out disableCors)) { configuration.DisableCors = disableCors; } var allowOrigin = cors.GetElementsByTagName("allowOrigin").Item(0); if (allowOrigin != null) { configuration.AllowOrigin = allowOrigin.InnerText; } return configuration; }
/// <summary> /// Creates a new bootstrapper with store and system access goverened by the specified providers. /// </summary> /// <param name="brightstarService">The connection to the BrightstarDB stores</param> /// <param name="authenticationProviders">An enumeration of the authentication providers to be used by the service</param> /// <param name="storePermissionsProvider">The store permissions provider to be used by the service</param> /// <param name="systemPermissionsProvider">The system permissions provider to be used by the service</param> /// <param name="corsConfiguration">The CORS configuration options for the service</param> /// <param name="rootPath">The path to the directory containing the service Views and assets folder</param> /// <exception cref="ArgumentNullException">Raised if any of the arguments to the method other than <paramref name="rootPath"/> are Null.</exception> public BrightstarBootstrapper( IBrightstarService brightstarService, IEnumerable<IAuthenticationProvider> authenticationProviders, AbstractStorePermissionsProvider storePermissionsProvider, AbstractSystemPermissionsProvider systemPermissionsProvider, CorsConfiguration corsConfiguration, string rootPath = null) { if (brightstarService == null) throw new ArgumentNullException("brightstarService"); if (authenticationProviders == null) throw new ArgumentNullException("authenticationProviders"); if (storePermissionsProvider == null) throw new ArgumentNullException("storePermissionsProvider"); if (systemPermissionsProvider == null) throw new ArgumentNullException("systemPermissionsProvider"); if (corsConfiguration == null) throw new ArgumentNullException("corsConfiguration"); _brightstarService = brightstarService; _authenticationProviders = authenticationProviders; _storePermissionsProvider = storePermissionsProvider; _systemPermissionsProvider = systemPermissionsProvider; _corsConfiguration = corsConfiguration; _rootPathProvider = (rootPath == null ? new DefaultRootPathProvider() : new FixedRootPathProvider(rootPath) as IRootPathProvider); }