Esempio n. 1
0
        private async Task <AuthenticationResult> Authenticate(HttpContext httpContext, Common.IanvsContext ianvsContext,
                                                               IIanvsConfigurationStore ianvsConfiguration, AuthenticatorFactory authenticatorFactory)
        {
            // If multiple schemes are defined on the operation, only one can apply to the request; check which one
            foreach (Ianvs::SecurityRequirement securityRequirement in ianvsContext.Security)
            {
                Ianvs::SecurityScheme schemeDefinition = ianvsConfiguration.SecuritySchemes?
                                                         .Find(s => s.Name == securityRequirement.SchemeName);
                if (schemeDefinition != null)
                {
                    ianvsContext.SecurityScheme = schemeDefinition;
                    IAuthenticationHandler authenticator = authenticatorFactory.GetAuthenticator(ianvsContext.SecurityScheme);
                    if (authenticator.CanAuthenticate(httpContext, ianvsContext))
                    {
                        ianvsContext.SecurityRequirement = securityRequirement;
                        return(await authenticator.Authenticate(httpContext, ianvsContext));
                    }
                    ianvsContext.SecurityScheme = null;
                }
            }

            // Couldn't apply security requirements
            return(new AuthenticationResult()
            {
                Authenticated = false,
                Error = "No Matching Security Scheme"
            });
        }