private static AuthenticationInternalResult Authenticate(HttpContext httpContext, AuthenticationRequiredAttribute authAttribute, ICustomAttributeProvider attributeProvider)
        {
            CustomAuthenticatorsAttribute[] customAuthenticators = null;
            if (attributeProvider is TypeInfo controllerType)
            {
                customAuthenticators = GetCustomAuthenticators(controllerType);
            }
            else
            {
                customAuthenticators = attributeProvider.GetAttributes <CustomAuthenticatorsAttribute>(false);
            }
            AuthenticationInternalResult result = null;
            bool scanInherit = attributeProvider is TypeInfo;

            switch (authAttribute.Policy)
            {
            case AuthenticationPolicy.NoAuthentication:
                return(new AuthenticationInternalResult(true, null, null, null));

            case AuthenticationPolicy.All:
            {
                if (customAuthenticators.Length > 0)
                {
                    Dictionary <CustomAuthenticatorExecutionPolicy, List <CustomAuthenticatorsAttribute> > authenticatorsGroups = GroupHelper.GroupBy(customAuthenticators, ag => ag.ExecutionPolicy);
                    result = TryAuthenticate(authenticatorsGroups, CustomAuthenticatorExecutionPolicy.BeforeCAS);
                    if (result != null)
                    {
                        return(result);
                    }

                    result = ExecuteCAS(httpContext);
                    if (result != null)
                    {
                        return(result);
                    }

                    result = TryAuthenticate(authenticatorsGroups, CustomAuthenticatorExecutionPolicy.AfterCAS);
                    if (result != null)
                    {
                        return(result);
                    }
                }
                else
                {
                    result = ExecuteCAS(httpContext);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }
            break;

            case AuthenticationPolicy.CASOnly:
            {
                result = ExecuteCAS(httpContext);
                if (result != null)
                {
                    return(result);
                }
            }
            break;

            case AuthenticationPolicy.DeclaredOnly:
            {
                if (customAuthenticators.Length > 0)
                {
                    Dictionary <CustomAuthenticatorExecutionPolicy, List <CustomAuthenticatorsAttribute> > authenticatorsGroups = GroupHelper.GroupBy(customAuthenticators, ag => ag.ExecutionPolicy);
                    result = TryAuthenticate(authenticatorsGroups, CustomAuthenticatorExecutionPolicy.BeforeCAS);
                    if (result != null)
                    {
                        return(result);
                    }

                    result = TryAuthenticate(authenticatorsGroups, CustomAuthenticatorExecutionPolicy.AfterCAS);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }
            break;
            }

            return(null);

            AuthenticationInternalResult TryAuthenticate(Dictionary <CustomAuthenticatorExecutionPolicy, List <CustomAuthenticatorsAttribute> > groups, CustomAuthenticatorExecutionPolicy policy)
            {
                AuthenticationInternalResult tryResult;

                if (groups.TryGetValue(policy, out List <CustomAuthenticatorsAttribute> group))
                {
                    foreach (CustomAuthenticatorsAttribute auths in group)
                    {
                        tryResult = auths.Authenticate(httpContext);
                        if (tryResult != null)
                        {
                            return(tryResult);
                        }
                    }
                    return(null);
                }
                else
                {
                    return(null);
                }
            }
        }
Esempio n. 2
0
 public AuthenticationDeclarationInfo(AuthenticationDeclaration declaration, AuthenticationRequiredAttribute attribute)
 {
     Declaration = declaration;
     Attribute   = attribute;
 }