Пример #1
0
 /// <summary>
 /// Create token provider.
 /// </summary>
 /// <param name="ctx"></param>
 /// <param name="store"></param>
 /// <param name="config"></param>
 /// <param name="logger"></param>
 public UserOrServiceTokenProvider(IHttpContextAccessor ctx, ITokenCacheProvider store,
                                   IClientConfig config, ILogger logger)
 {
     _ctx      = ctx ?? throw new ArgumentNullException(nameof(ctx));
     _logger   = logger ?? throw new ArgumentNullException(nameof(logger));
     _userAuth = new BehalfOfTokenProvider(ctx, store, config, logger);
     _svcAuth  = new AppAuthenticationProvider(config);
 }
Пример #2
0
 public TokenService(
     IOptions <AzureAdOptions> options,
     ITokenCacheProvider tokenCacheProvider
     )
 {
     azureAdOptions          = options.Value;
     this.tokenCacheProvider = tokenCacheProvider;
 }
 /// <summary>
 /// Constructor of the TokenAcquisition service. This requires the Azure AD Options to
 /// configure the confidential client application and a token cache provider.
 /// This constructor is called by ASP.NET Core dependency injection
 /// </summary>
 /// <param name="options">Options to configure the application</param>
 public TokenAcquisition(ITokenCacheProvider tokenCacheProvider, IConfiguration configuration)
 {
     azureAdOptions = new AzureADOptions();
     configuration.Bind("AzureAD", azureAdOptions);
     _applicationOptions = new ConfidentialClientApplicationOptions();
     configuration.Bind("AzureAD", _applicationOptions);
     this.tokenCacheProvider = tokenCacheProvider;
 }
        /// <summary>
        /// Create device code provider with callback
        /// </summary>
        /// <param name="store"></param>
        /// <param name="logger"></param>
        /// <param name="callback"></param>
        /// <param name="config"></param>
        public DeviceCodeTokenProvider(Action <string, DateTimeOffset, string> callback,
                                       IClientConfig config, ITokenCacheProvider store, ILogger logger)
        {
            _logger   = logger ?? throw new ArgumentNullException(nameof(logger));
            _config   = config ?? throw new ArgumentNullException(nameof(config));
            _callback = callback ?? throw new ArgumentNullException(nameof(callback));
            _store    = store ?? DefaultTokenCacheProvider.Instance;

            if (string.IsNullOrEmpty(_config.AppId))
            {
                _logger.Error("Device code token provider was not configured with " +
                              "a client id.  No tokens will be obtained.");
            }
        }
Пример #5
0
        /// <summary>
        /// Create auth provider. Need to also inject the http context accessor
        /// to be able to get at the http context here.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="store"></param>
        /// <param name="config"></param>
        /// <param name="logger"></param>
        public BehalfOfTokenProvider(IHttpContextAccessor ctx, ITokenCacheProvider store,
                                     IClientConfig config, ILogger logger)
        {
            _store  = store ?? throw new ArgumentNullException(nameof(store));
            _ctx    = ctx ?? throw new ArgumentNullException(nameof(ctx));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            _config = config ?? throw new ArgumentNullException(nameof(config));

            if (string.IsNullOrEmpty(_config.AppId) ||
                string.IsNullOrEmpty(_config.AppSecret))
            {
                _logger.Error("On behalf token provider was not configured with " +
                              "a client id or secret.  No tokens will be obtained. ");
            }
        }
        /// <summary>
        /// Create auth provider. Need to also inject the http context accessor
        /// to be able to get at the http context here.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="store"></param>
        /// <param name="config"></param>
        /// <param name="logger"></param>
        /// <param name="handler"></param>
        /// <param name="acquireTokenIfSilentFails"></param>
        public BehalfOfTokenProvider(IHttpContextAccessor ctx, ITokenCacheProvider store,
                                     IClientConfig config, ILogger logger, IAuthenticationErrorHandler handler = null, bool acquireTokenIfSilentFails = false)
        {
            _store   = store ?? throw new ArgumentNullException(nameof(store));
            _ctx     = ctx ?? throw new ArgumentNullException(nameof(ctx));
            _logger  = logger ?? throw new ArgumentNullException(nameof(logger));
            _config  = config ?? throw new ArgumentNullException(nameof(config));
            _handler = handler ?? new ThrowHandler();
            _acquireTokenIfSilentFails = acquireTokenIfSilentFails;

            if (string.IsNullOrEmpty(_config.AppId) ||
                string.IsNullOrEmpty(_config.AppSecret))
            {
                _logger.Error("On behalf token provider was not configured with " +
                              "a client id or secret.  No tokens will be obtained. ");
            }
        }
        /// <summary>
        /// Helper to create authentication context
        /// </summary>
        /// <param name="authorityUrl"></param>
        /// <param name="tenantId"></param>
        /// <param name="store"></param>
        /// <returns></returns>
        private static AuthenticationContext CreateAuthenticationContext(
            string authorityUrl, string tenantId, ITokenCacheProvider store)
        {
            if (string.IsNullOrEmpty(authorityUrl))
            {
                authorityUrl = kDefaultAuthorityUrl;
            }
            var uri = new UriBuilder(authorityUrl)
            {
                Path = tenantId ?? "common"
            };
            var ctx = new AuthenticationContext(uri.ToString(),
                                                store.GetCache(authorityUrl));

            if (tenantId == null && ctx.TokenCache.Count > 0)
            {
                uri.Path = ctx.TokenCache.ReadItems().First().TenantId;
                ctx      = new AuthenticationContext(uri.ToString());
            }
            return(ctx);
        }
Пример #8
0
 public TokenProvider(ILogger <TokenProvider> logger, IOptionsSnapshot <AzureAdConfiguration> adOptions, ITokenCacheProvider tokenCacheProvider)
 {
     _logger             = logger ?? throw new ArgumentNullException(nameof(logger));
     _adOptions          = adOptions ?? throw new ArgumentNullException(nameof(adOptions));
     _tokenCacheProvider = tokenCacheProvider ?? throw new ArgumentNullException(nameof(tokenCacheProvider));
 }
 /// <summary>
 /// Constructor of the TokenAcquisition service. This requires the Azure AD Options to
 /// configure the confidential client application and a token cache provider.
 /// This constructor is called by ASP.NET Core dependency injection
 /// </summary>
 /// <param name="options">Options to configure the application</param>
 public TokenAcquisition(ITokenCacheProvider tokenCacheProvider, IConfiguration configuration)
 {
     _azureAdOptions = new AzureADOptions();
     configuration.Bind("AzureAD", _azureAdOptions);
     _tokenCacheProvider = tokenCacheProvider;
 }
Пример #10
0
 public Global WithTokenCacheProvider(ITokenCacheProvider tokenProvider)
 {
     this._tokenProvider = tokenProvider;
     return(this);
 }
 /// <summary>
 /// Create console output device code based token provider
 /// </summary>
 /// <param name="store"></param>
 /// <param name="logger"></param>
 /// <param name="config"></param>
 public DeviceCodeTokenProvider(IClientConfig config, ITokenCacheProvider store,
                                ILogger logger) :
     this((c, exp, msg) => Console.WriteLine(msg), config, store, logger)
 {
 }
 /// <inheritdoc/>
 public CliAuthenticationProvider(IDeviceCodePrompt prompt,
                                  IClientConfig config, ITokenCacheProvider store, ILogger logger)
 {
     _vs = new VsAuthenticationProvider(config);
     _dc = new DeviceCodeTokenProvider(prompt, config, store, logger);
 }
 /// <summary>
 /// Create console output device code based token provider
 /// </summary>
 /// <param name="store"></param>
 /// <param name="config"></param>
 /// <param name="logger"></param>
 public DeviceCodeTokenProvider(IClientConfig config, ITokenCacheProvider store,
                                ILogger logger) :
     this(new ConsolePrompt(), config, store, logger)
 {
 }