Trouble redirecting to an error page from my custom ActionFilterAttribute

RedirectResult from ActionFilterAttribute

I made a custom ActionFilterAttribute which checks a URL to see if the domains are contained in a whitelist of domains. It's working, but I need to redirect to a failure page on failure.

Can I use the following call?

filterContext.Result = new RedirectResult("/error/Unauthorized");

and what about the path?

I see

ErrorController : Controller
 
with the following action method

public ActionResult Unauthorized

and it returns the following view:

            return View("UserNoPermissions");

and I see the Shared View

UserNoPermissions.cshtml

yet, I get the error:


HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Suggestions?
newbiewebSr. Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ambienceCommented:
See https://docs.microsoft.com/en-us/aspnet/core/security/cors for the preferred way of implementing whitelists. Its much easier and most work has already been done. The link is for MVC5 but easy to find similar for older versions.

As for your current solution, are you inheriting your actionfilter from an AuthorizeAttribute?

Instead of a redirectresult try throwing and exception

throw new HttpException(401, "Unauthoized");

Open in new window


Here is implementation of custom AuthroizeAttribute's AuthorizeCore method. Should be enough to give idea

protected override bool AuthorizeCore(HttpContextBase httpContext)
{
    if (httpContext == null)
        throw new ArgumentNullException("httpContext");

    string userIpAddress = httpContext.Request.UserHostAddress;

    try
    {
        // Local requests are always allowed
        if (httpContext.Request.IsLocal)
        {
            logger.Debug("Allowing local request without further checking");
            return true;
        }

        if (UseConfigurationFile)
        {
            Mode = ConfigurationManager.AppSettings.EnumValue(ConfigurationKeyMode, RestrictionMode.AllowAll);
        }

        // Check that the IP is allowed to access
        bool ipAllowed = CheckAllowedIPs(userIpAddress);
        if (Mode == RestrictionMode.DenyAll)
        {
            logger.Debug("IP: {0}, allowed: {1}", userIpAddress, ipAllowed);
            return ipAllowed;
        }

        // Check that the IP is not denied access
        bool ipDenied = CheckDeniedIPs(userIpAddress);
        if (Mode == RestrictionMode.AllowAll)
        {
            logger.Debug("IP: {0}, denied: {1}", userIpAddress, ipDenied);
            return !ipDenied;
        }

        // Only allowed if allowed and not denied
        bool finallyAllowed = ipAllowed && !ipDenied;

        if(!finallyAllowed)
            logger.Debug("IP: {0}, allowed: {1}, denied: {2}", userIpAddress, ipAllowed, ipDenied); 

        return finallyAllowed;
    }
    catch (Exception)
    {
        // Log the exception, probably something wrong with the configuration
    }

    return true; 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbiewebSr. Software EngineerAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.