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 ...