【发布时间】:2019-03-25 11:31:57
【问题描述】:
在我的帐户控制器中,在登录操作中,有以下代码:
case "Sucess":
string rule = CheckRule(model.username, model.Password);
Response.SetCookie(SetAuthCookie(model.username, model.RememberMe, rule));
return RedirectToAction("Index", rule);
在检查规则中,我返回带有另一个控制器名称的字符串,其中包括 Admin 和 BasicUser,以下是它们的代码:
{
[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");
}
}
和:
{
[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");
}
}
}
在 RouteConfig 中:
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 }
);
如果我使用管理员用户登录,它可以完美运行,但是使用基本用户,navagador 不会重定向到路由,只是登录屏幕,如果我输入控制器地址就可以了。 我添加了一个标签 html 来查看 cookie 用户数据,这很好用(显示 de BasicUserRule)。
对不起,如果我的问题不是很清楚,我是新手......
【问题讨论】:
标签: c# asp.net-mvc-5