コード例 #1
0
        public ClientCredentialsTokenService(IOptionsMonitor <ClientCredentialsOptions> options,
                                             IHttpClientFactory httpClientFactory, ILogger <ClientCredentialsTokenService> logger)
        {
            Options            = options.CurrentValue;
            _httpClientFactory = httpClientFactory;
            _logger            = logger;

            Task.Run(() => GetJsonWebKeyAsync()).Wait();
        }
コード例 #2
0
        /// <summary>
        /// Sets up the SecureTokenService and BearerTokenHandler via
        /// <code>
        /// services.AddSecureTokenService<ClientCredentialsTokenService>(Configuration);
        /// </code>
        /// Note: you need to include a Security:ClientCredentials section in
        /// configuration.
        /// Note: you need to call app.UseAuthentication in Configure, as well.
        /// </summary>
        /// <typeparam name="TTokenService"></typeparam>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <param name="configKey"></param>
        /// <returns></returns>
        public static IServiceCollection AddSecureTokenService <TTokenService>(this IServiceCollection services,
                                                                               IConfiguration config, string configKey = "Security:ClientCredentials")
            where TTokenService : class, ITokenService
        {
            services.AddSingleton <ITokenService, TTokenService>();

            if (typeof(ClientCredentialsTokenService).IsAssignableFrom(typeof(TTokenService)))
            {
                var options       = new ClientCredentialsOptions();
                var configSection = config.GetSection(configKey);
                configSection.Bind(options);

                services.Configure <ClientCredentialsOptions>(opt =>
                {
                    opt.Authority    = options.Authority;
                    opt.ClientId     = options.ClientId;
                    opt.ClientSecret = options.ClientSecret;
                    opt.Scopes       = options.Scopes;
                });

                if (options.Authority == null)
                {
                    throw new Exception($"Not able to bind Configuration[\"{configKey}\"] to ClientCredentialsOptions");
                }

                services.AddHttpClient("ClientCredentialsTokenService", configure =>
                {
                    configure.BaseAddress = new Uri(options.Authority);
                });

                //TODO: See if this is needed
                //services.AddAuthentication("Bearer")
                //    .AddScheme<BearerTokenOptions, BearerTokenHandler>("Bearer", opt => { });
            }

            return(services);
        }
コード例 #3
0
 public OneTimeTokenService(ClientCredentialsOptions options)
 {
     Options = options;
 }