Search code examples
asp.net-mvc-3elmah

How to set a custom route value so that only members with the Admin role can access my elmah.axd page in MVC3


I need to set a custom route value so that you are forced to go to

site/admin/elmah.axd

In my web.config I have:

<location path="elmah.axd">
 <system.web>
   <authorization>
     <allow roles="Admin" />
     <deny users="*" />
   </authorization>
 </system.web>
</location>

Plus all the other pertinent elmah settings. If I remove that section I can just type:

site/elmah.axd

But I only want Admin's to be able to access this page.

I have an admin page that you login and if you are in the Admin Role then you should be redirected to elmah.axd

    public AdminController(IRepository<User> userRepository)
    {
        _userRepository = userRepository;
    }

    //
    // GET: /Admin/

    public ActionResult Index()
    {
        return View(Constants.Admin);
    }

public ActionResult AdminLogin(string SSN)
    {
        var user = _userRepository.FindBy(x => x.UserName == SSN).SingleOrDefault();
        if (UserIsAnAdmin(user))
        {
            return RedirectToRoute("/elmah.axd");
        }
        return View(Constants.DeniedAccess);
    }

I have a route value in my Global.asax file that I need help with.

 routes.MapRoute(
            "mocklogin",
            "{controller}/{action}", // URL with parameters
            new { controller = "MockLogin", action = "Index" } // Parameter defaults
            );


        routes.MapRoute(
            "elmah.axd",
            "{controller}/{action}", // URL with parameters

            );



        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}", // URL with parameters
            //need something here redirecting to the elmah.axd page      
    );

Is there a way to redirect the page to the /elmah.axd page on my site.

In the end I want to go to the Admin page of my site and when I click submit if the User is an admin I want to redirect to the elmah.axd page. Otherwise I go to my custom error page. I need to somehow get the ssn on the controller call the conditional and if true redirect to elmah.axd. How do I redirect to elmah.axd in MVC?


Solution

  • Update : To redirect to a static route use Response.Redirect("URL");

    see below :

    You can't create a route that point to something else than a Controller and Action. So the best would be that your Index Action of your Controller does a redirect to /elmah.axd and set your default route to this action.

    Route :

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}", // URL with parameters
    

    );

    Controller :

        public AdminController(IRepository<User> userRepository)
        {
            _userRepository = userRepository;
        }
    
        public ActionResult Index()
        {
             Response.Redirect("/elmah.axd");
             return View(); //will not be hit
        }
    
        public ActionResult AdminLogin(string SSN)
        {
            var user = _userRepository.FindBy(x => x.UserName == SSN).SingleOrDefault();
            if (UserIsAnAdmin(user))
            {
                return RedirectToRoute("/elmah.axd");
            }
            return View(Constants.DeniedAccess);
        }
    

    Let me know if it works.