public static IServiceCollection AddApiAuthorization(this IServiceCollection services, Action <ApiAuthorizationOptions> configureOptions)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (configureOptions == null)
            {
                throw new ArgumentNullException(nameof(configureOptions));
            }

            var options = new ApiAuthorizationOptions();

            configureOptions(options);


            if (options.PathMatch.HasValue && options.PathMatch.Value.EndsWith("/", StringComparison.Ordinal))
            {
                throw new ArgumentException("The path must not end with a '/'", nameof(options.PathMatch));
            }


            services.Configure(configureOptions);

            return(services);
        }
        ///// <summary>
        ///// Creates a new instance of the ApiAuthorizedMiddleware.
        ///// </summary>
        ///// <param name="next">The next middleware in the pipeline.</param>
        ///// <param name="hostingEnv">The <see cref="IHostingEnvironment"/> used by this middleware.</param>
        ///// <param name="options">The configuration options.</param>
        ///// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
        public ApiAuthorizationMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv, IOptions <ApiAuthorizationOptions> options, ILoggerFactory loggerFactory, IKardSession session)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            if (hostingEnv == null)
            {
                throw new ArgumentNullException(nameof(hostingEnv));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _next    = next;
            _options = options.Value;
            _logger  = loggerFactory.CreateLogger <ApiAuthorizationMiddleware>();
            _session = session;
        }
Esempio n. 3
0
        public static IApplicationBuilder UseApiAuthorization(this IApplicationBuilder app, ApiAuthorizationOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.PathMatch.HasValue && options.PathMatch.Value.EndsWith("/", StringComparison.Ordinal))
            {
                throw new ArgumentException("The path must not end with a '/'", nameof(options.PathMatch));
            }


            return(app.UseMiddleware <ApiAuthorizationMiddleware>(Options.Create(options)));
        }