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

Asked

Viewed 321 times

1

In my Account controller, in Action Login, I have the code below:

                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 of these controllers:

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 code of Route config:

 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 simply 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 basicuser’s Index.cshtml to see the rule that appears in the cookie, and the right rule appears, only it is not redirecting to the controller page .

I’m sorry if I haven’t been very clear, I’m very Newbie yet...

1 answer

2

Hey guys fia the same question in Stack in English, I already received the answer and I was successful in correcting this error: My problem was in setting the routes I redid 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. It is certain that I need to study more this question of routes before putting this in production, I do not want to be exposed to failures...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.