MVC RedirectToRouteResult Help
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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