Ejemplo n.º 1
0
        public static bool CanModuleProcess(HttpContext context)
        {
            // check if the current module requires authentication
            // if authentication is not required then proceed
            if (RequriresAuthentication(context) == false)
            {
                return(true);
            }

            // check if the request is authenticated
            bool authenticated = IsAuthenticated(context);

            if (authenticated == false)
            {
                // if not authenticated then try to authenticate the request
                authenticated = Authenticate(context);
                if (authenticated == false)
                {
                    // clear cookie
                    context.Response.Cookies.Delete("X-App-Key");
                    context.Response.Cookies.Delete("Authorization");

                    // authentication failed
                    context.Response.StatusCode = StatusCodes.Status403Forbidden;
                    return(false);
                }
                else
                {
                    // if authentication is successful then return the angular config
                    IModule module = (IModule)context.Items["module"];
                    string  result = module.Authenticated(context);
                    if (result != null)
                    {
                        context.Response.WriteAsync(result);
                    }
                    return(false);
                }
            }

            // check if the request is authorized
            bool authorized = IsAuthorized(context);

            if (authorized == false)
            {
                // request is not authorized
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                return(false);
            }

            // check if it is requesting for validate
            if (context.Request.Headers.ContainsKey("Validate"))
            {
                // if authentication is successful then return the angular config
                IModule module = (IModule)context.Items["module"];
                string  result = module.Authenticated(context);
                if (result != null)
                {
                    context.Response.WriteAsync(result);
                }
                return(false);
            }

            return(true);
        }