Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. MVC RedirectToRouteResult Help

MVC RedirectToRouteResult Help

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestionasp-netdatabase
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Foothill
    wrote on last edited by
    #1

    I am trying to build a dictionary of RedirectToRouteResults. Background: I am building a web application. Once a user gets past the login, the server returns an encrypted session cookie that follows the user around and is used to display user specific information on each page. Pretty standard fair. Now, the problem is that each Controller Action performs a check on the cookie to check for tampering, if the user's session has expired, if the user is authorized for this page, etc... so each controller has to handle a half-dozen different return types from the security check function. I could handle this with a switch...case in the catch block of each controller's code but that seems sloppy. The Question: I am having trouble figuring out how I can build a Dictionary of redirect routes to simplify this. I haven't been able to find anything on the internet in the way of an example for properly building a RedirectToRouteResult object that can be returned in a controller. Instead of this:

    public ActionResult Index()
    {
    SecurityCheck secCheck = ValidateConnection(this);

    if (secCheck != secCheck.Okay)
    {
    switch (secCheck)
    {
    case SecurityCheck.AuthenticationFailure:
    return RedirectToAction("Default", "ErrorHandler", new { message = "Not Authorized." });
    // and so on....
    }
    }
    }

    I would like to simplify it to:

    public ActionResult Index()
    {
    SecurityCheck secCheck = ValidateConnection(this);

    if (secCheck != secCheck.Okay)
    {
    return SecurityRedirect.Routes[secCheck];
    // SecurityRedirect would be a static class that would contain
    // a dictonary of the how each security issue should be redirected.
    }
    else
    {
    return View();
    }
    }

    Is there a way? I am having no luck figuring out how to build a RedirectToRouteResult as the documentation on it is Spartan at best. I do believe that they would accomplish my goal.

    if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

    Richard DeemingR 1 Reply Last reply
    0
    • F Foothill

      I am trying to build a dictionary of RedirectToRouteResults. Background: I am building a web application. Once a user gets past the login, the server returns an encrypted session cookie that follows the user around and is used to display user specific information on each page. Pretty standard fair. Now, the problem is that each Controller Action performs a check on the cookie to check for tampering, if the user's session has expired, if the user is authorized for this page, etc... so each controller has to handle a half-dozen different return types from the security check function. I could handle this with a switch...case in the catch block of each controller's code but that seems sloppy. The Question: I am having trouble figuring out how I can build a Dictionary of redirect routes to simplify this. I haven't been able to find anything on the internet in the way of an example for properly building a RedirectToRouteResult object that can be returned in a controller. Instead of this:

      public ActionResult Index()
      {
      SecurityCheck secCheck = ValidateConnection(this);

      if (secCheck != secCheck.Okay)
      {
      switch (secCheck)
      {
      case SecurityCheck.AuthenticationFailure:
      return RedirectToAction("Default", "ErrorHandler", new { message = "Not Authorized." });
      // and so on....
      }
      }
      }

      I would like to simplify it to:

      public ActionResult Index()
      {
      SecurityCheck secCheck = ValidateConnection(this);

      if (secCheck != secCheck.Okay)
      {
      return SecurityRedirect.Routes[secCheck];
      // SecurityRedirect would be a static class that would contain
      // a dictonary of the how each security issue should be redirected.
      }
      else
      {
      return View();
      }
      }

      Is there a way? I am having no luck figuring out how to build a RedirectToRouteResult as the documentation on it is Spartan at best. I do believe that they would accomplish my goal.

      if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Foothill wrote:

      each Controller Action performs a check on the cookie

      Sounds like you want an action filter[^] instead. :)


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      F 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        Foothill wrote:

        each Controller Action performs a check on the cookie

        Sounds like you want an action filter[^] instead. :)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        F Offline
        F Offline
        Foothill
        wrote on last edited by
        #3

        Is it possible to pass an object created by an authorization filter to the controller action? My authentication processing creates several objects that get used by most controller actions.

        if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

        Richard DeemingR 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Foothill wrote:

          each Controller Action performs a check on the cookie

          Sounds like you want an action filter[^] instead. :)


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          F Offline
          F Offline
          Foothill
          wrote on last edited by
          #4

          Okay, so I have made some progress but I am running into a bit of trouble. On a lot of failures, I redirect the user to the login page but the page URL is not being populated so it tries to call my login action on whatever page its on resulting in an error.

          // Login redirect handler
          private ViewResult GetRedirectToLogin()
          {
          ViewResult result = new ViewResult();

          result.ViewName = "~/Views/Login/Index.cshtml";
          result.MasterName = "~/Views/Shared/_loginlayout.cshtml";
          result.TempData = _context.Controller.TempData;

          return result;
          }

          This has been working. Any errors with authentication are caught and the user is redirected. The problem is that the browser still thinks its on the first page so that when I click my "Login" button the server looks for a [HttpPost]Login action and returns a 404 error.

          if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

          Richard DeemingR 1 Reply Last reply
          0
          • F Foothill

            Okay, so I have made some progress but I am running into a bit of trouble. On a lot of failures, I redirect the user to the login page but the page URL is not being populated so it tries to call my login action on whatever page its on resulting in an error.

            // Login redirect handler
            private ViewResult GetRedirectToLogin()
            {
            ViewResult result = new ViewResult();

            result.ViewName = "~/Views/Login/Index.cshtml";
            result.MasterName = "~/Views/Shared/_loginlayout.cshtml";
            result.TempData = _context.Controller.TempData;

            return result;
            }

            This has been working. Any errors with authentication are caught and the user is redirected. The problem is that the browser still thinks its on the first page so that when I click my "Login" button the server looks for a [HttpPost]Login action and returns a 404 error.

            if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            How about:

            private ActionResult GetRedirectToLogin()
            {
            return new RedirectToRouteResult(new RouteValueDictionary
            {
            { "Controller", "Login" },
            { "Action", "Index" }
            });
            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            F 1 Reply Last reply
            0
            • F Foothill

              Is it possible to pass an object created by an authorization filter to the controller action? My authentication processing creates several objects that get used by most controller actions.

              if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Something like this[^] should work. If you add the data to the filterContext.RouteData.Values collection with a specific key, you should be able to receive it as a parameter to your action, where the parameter name matches the key.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              F 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Something like this[^] should work. If you add the data to the filterContext.RouteData.Values collection with a specific key, you should be able to receive it as a parameter to your action, where the parameter name matches the key.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                F Offline
                F Offline
                Foothill
                wrote on last edited by
                #7

                I ended up wrapping the Controller class in a wrapper class with Properties for the data that I needed to pass along. Problem solved.

                if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  How about:

                  private ActionResult GetRedirectToLogin()
                  {
                  return new RedirectToRouteResult(new RouteValueDictionary
                  {
                  { "Controller", "Login" },
                  { "Action", "Index" }
                  });
                  }


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  F Offline
                  F Offline
                  Foothill
                  wrote on last edited by
                  #8

                  I found that one. Works like a charm. Thanks for your help on this one. I had never even looked at Action Filters before this.

                  if (Object.DividedByZero == true) { Universe.Implode(); } Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups