예제 #1
0
        public static IServiceCollection AddServiceAuthorize(this IServiceCollection services, Action <ServiceAuthorizeOptions> configure)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            services.Configure(configure);

            var options = new ServiceAuthorizeOptions();

            configure(options);

            services.AddAuthentication(o =>
            {
                o.AllowClientToken = options.AllowClientToken;
                o.AllowUserToken   = options.AllowUserToken;
                o.ValidAudiences   = options.ValidAudiences;
                o.Authority        = options.Authority;
                o.ValidIssuer      = options.ValidIssuer;
                o.ValidateAudience = options.ValidateAudience;
                o.ValidateIssuer   = options.ValidateIssuer;
            });

            services.AddAuthorizeTokenClient(o =>
            {
                o.ClientId     = options.ClientId;
                o.ClientSecret = options.ClientSecret;
            }, new Uri(options.Authority));

            services.Configure <MvcOptions>(o => o.Filters.Add <ServiceAuthorizeFilter>());

            services.AddSingleton <ServiceAuthorizeFilter>();

            return(services);
        }
예제 #2
0
 public ServiceAuthorizeFilter(IAuthorizeTokenClient serviceAuthorizeHttpClient, IOptions <ServiceAuthorizeOptions> options)
 {
     _serviceAuthorizeHttpClient = serviceAuthorizeHttpClient;
     _options = options?.Value ?? throw new ArgumentNullException(nameof(options));
 }