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


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.

newbiewebSr. Software EngineerAsked:
Who is Participating?
ambienceConnect With a Mentor Commented:
See 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;

        // 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;

            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

newbiewebSr. Software EngineerAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.