MVC5 (Asp.Net4.5.2) - RedirectToAction does not work in a single case

0

In my Controller Account, in Login Action, I have the following code:

                case "Sucess":
                    string rule = CheckRule(model.username, model.Password);
                    Response.SetCookie(SetAuthCookie(model.username, model.RememberMe, rule));
                    return RedirectToAction("Index", rule);

In checkrule, I return a string with the name of the controller according to the authentication rule, between these names are Admin and BasicUser, below is the code for these drivers:

Admin

{
[Authorize]
public class AdminController : Controller
{
    private bool attAuthor = isAuthorized();
    private bool attAuth = isAuthenticated();
    private string rule = returnrule();
    // GET: Admin
    public ActionResult Index()
    {
        if (!attAuthor)
        {
            return RedirectToAction("erro401",rule);
        }
        else
        {
            return View();
        }


    }

    public ActionResult erro401()
    {
        return View("erro401");
    }

}

and BasicUser:

{
[Authorize]
public class BasicUserController : Controller
{
    private bool attAuthor = isAuthorized();
    private bool attAuth = isAuthenticated();
    private string rule = returnrule();
    // GET: BasicUser
    public ActionResult Index()
    {
         if (!attAuthor)
        {
            return RedirectToAction("erro401", rule);
        }
        else
        {
            FormsAuthenticationTicket authticket = get_ticket();
            string str = rule + " / " + authticket.Name;
            ViewBag.Htmlstr = str;
            return View();
        }


    }

    public ActionResult erro401()
    {
        return View("erro401");
    }


}

}

In the Route config code:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
           name: "Default",
           url: "",
           defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
       );

        routes.MapRoute(
            name: "BasicUser",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "BasicUser", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
             name: "Admin",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
         );

If I log in with Admin user it works, but if I log in with a basicuser the browser does not redirect, it just stays on the login screen, but if I type in the address bar it goes to the right page. I added a tag in the Index.cshtml of the basicuser to see the rule that appears in the cookie, and the right rule appears, not just redirecting it to the controller page.

Sorry if I was not very clear, I'm very newbie yet ...

    
asked by anonymous 31.08.2016 / 23:58

1 answer

0

Hey folks, the same question on the Stack in English, I already received the answer and I was successful in correcting this error: My problem was in the configuration of routes I have refactored the routes with the code below:

 routes.MapRoute(
                name: "BasicUser",
                url: "BasicUser/{action}/",
                defaults: new { controller = "BasicUser", action = "Index" }
            );
            routes.MapRoute(
                 name: "Admin",
                 url: "Admin/{action}/",
                 defaults: new { controller = "Admin", action = "Index"}
             );

The url pattern was "/{controller}/{action}/" now I point directly to the controller. Certainly I need to study this issue of routes before it is in production, I do not want to be exposed to failures ...

    
01.09.2016 / 20:39