コード例 #1
0
        /// <summary>
        /// Authenticate for the specified authentication scheme.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/>.</param>
        /// <param name="scheme">The name of the authentication scheme.</param>
        /// <returns>The result.</returns>
        public virtual async Task <AuthenticateResult> AuthenticateAsync(AuthenticateContext context, string scheme)
        {
            if (scheme == null)
            {
                var defaultScheme = await Schemes.GetDefaultAuthenticateSchemeAsync();

                scheme = defaultScheme?.Name;
                if (scheme == null)
                {
                    throw new InvalidOperationException($"No authenticationScheme was specified, and there was no DefaultAuthenticateScheme found.");
                }
            }

            var handler = await Handlers.GetHandlerAsync(context, scheme);

            if (handler == null)
            {
                throw await CreateMissingHandlerException(scheme);
            }

            var result = await handler.AuthenticateAsync();

            if (result != null && result.Succeeded)
            {
                var transformed = await Transform.TransformAsync(result.Principal);

                return(AuthenticateResult.Success(new AuthenticationTicket(transformed, result.Properties, result.Ticket.AuthenticationScheme)));
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Returns the handler instance that will be used.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="authenticationScheme">The name of the authentication scheme being handled.</param>
        /// <returns>The handler instance.</returns>
        public async Task <IAuthenticationHandler> GetHandlerAsync(AuthenticateContext context, string authenticationScheme)
        {
            if (_handlerMap.ContainsKey(authenticationScheme))
            {
                return(_handlerMap[authenticationScheme]);
            }

            var scheme = await Schemes.GetSchemeAsync(authenticationScheme);

            if (scheme == null)
            {
                return(null);
            }
            var handler = (context.RequestServices.GetService(scheme.HandlerType) ??
                           ActivatorUtilities.CreateInstance(context.RequestServices, scheme.HandlerType))
                          as IAuthenticationHandler;

            if (handler != null)
            {
                await handler.InitializeAsync(scheme, context);

                _handlerMap[authenticationScheme] = handler;
            }
            return(handler);
        }
コード例 #3
0
        /// <summary>
        /// Initialize the handler, resolve the options and validate them.
        /// </summary>
        /// <param name="scheme"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task InitializeAsync(AuthenticationScheme scheme, AuthenticateContext context)
        {
            if (scheme == null)
            {
                throw new ArgumentNullException(nameof(scheme));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Scheme  = scheme;
            Context = context;
            Options = OptionsMonitor.Get(Scheme.Name) ?? new TOptions();
            Options.Validate(Scheme.Name);

            await InitializeHandlerAsync();
        }
コード例 #4
0
 public static Task <AuthenticateResult> AuthenticateAsync(this AuthenticateContext context, string scheme) =>
 context.RequestServices.GetRequiredService <IAuthenticationService>().AuthenticateAsync(context, scheme);
コード例 #5
0
 public static Task <AuthenticateResult> AuthenticateAsync(this AuthenticateContext context) =>
 context.AuthenticateAsync(scheme: null);