Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deny direct URL access to action method

I'm trying to find a way to deny any direct access to my action methods. Basically what I want my users to click on links to navigate instead of typing the URL directly into the address bar in their browsers.

Now I know this can be done by checking the urlreferrer in the request object but this is kind of unreliable and weak because the urlreferrer can easily be modified and some of the security suites actually remove it from the request.

So does any of you know of a way to do this in asp.net mvc3?

like image 427
Duy Avatar asked Dec 01 '11 05:12

Duy


People also ask

How do you prevent a controller method from being accessed by an URL?

If you want to avoid a function in the controller to be seen in the url, simply make that function private/protected or prepend an _ in the function name.

How can we prevent direct URL access in MVC?

Apply this feature in FilterConfig. We have to call this feature under OnActionExecuting of Action filter. We have to apply filter as below written lines to prevent direct URL access in MVC. If we are tempering URL in browser then it will forcibly throw you to Logout action of Home Controller lying under Main area.

How do you prevent a partial view from being called directly from the URL?

Just add the [ChildActionOnly] above your Action and should prevent user from accesing directly.


3 Answers

Below is the code for the NoDirectAccessAttribute method to restrict direct access to any class or action method that applies the NoDirectAccess attribute

using System;
using System.Web.Mvc;
using System.Web.Routing;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class NoDirectAccessAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Request.UrlReferrer == null || 
                    filterContext.HttpContext.Request.Url.Host != filterContext.HttpContext.Request.UrlReferrer.Host)
            {
            filterContext.Result = new RedirectToRouteResult(new
                           RouteValueDictionary(new { controller = "Home", action = "Index", area = "" }));  
        }
    }
}

Use the action filter as follows for any controller or action method where you don't want user to request it directly

[NoDirectAccess]
public ActionResult IsUsernameUnique()

In the example above, any direct access to the IsUsernameUnique action method will automatically redirect the user to the Home/Index action method.

like image 124
user1642036 Avatar answered Sep 23 '22 18:09

user1642036


i am not sure but maybe this can help you Consider we have a page with this url

www.yoursite.com/home.aspx

To avoid your user to directly browse this page you can rewrite you url like this

www.yoursite.com/fdmf489ruv30/home.aspx

and in this url the part "fdmf489ruv30" is a unique string that you created it on session_start and will destroy it on session_end

like image 22
Ali Foroughi Avatar answered Sep 20 '22 18:09

Ali Foroughi


It is impossible to securely ensure that an authorized user CANNOT spoof a valid request.
After all, if the browser can create a "valid request", then so can an authorized user!

However, this is an unusual requirement, and I'm interested to know what's the motivation behind it?

There are, however, a number of ways to make it harder to spoof the request.
Since no methods will be completely secure, you can try to obfuscate and make it tedious for someone to spoof your requests.

As others have suggested, you could create a random "token" per session and require it in the URL (either as a path or as a querystring).

It would be even better to use JavaScript for this. Render a hidden input with this "token". Then, intercept each link's click event, append the "token" value to the URL, then navigate to the URL.

You can enhance the JavaScript to somehow "process" your token before using it, and minify your JavaScript to obfuscate it ... this would definitely deter even the above-average users from tinkering with your URLs.

There are tons of possible solutions, but the "right" solution really depends on what specific behavior you are trying to prevent.

like image 39
Scott Rippey Avatar answered Sep 23 '22 18:09

Scott Rippey