在我的账户控制器,在登录操作,有下面的代码:MVC5(Asp.Net4.5.2) - RedirectToAction在一种情况下一点儿也不回
case "Sucess":
string rule = CheckRule(model.username, model.Password);
Response.SetCookie(SetAuthCookie(model.username, model.RememberMe, rule));
return RedirectToAction("Index", rule);
在checkrule,我返回字符串的名字另一个控制器,其中有管理员和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 userdata,这工作正常(显示的BasicUserRule)。
很抱歉,如果我的问题不是很清楚,我是新手...
塞达特您好,感谢的答案,我会解决这些路线,但'HttpStatusCode.Unauthorized',在那里我可以配置此帐户登录自动响应?我把登录URL放在Web.config上的FormsAuthentication中:'
@RogérioFerreira应该足够了。这是默认行为。如果您想在操作前强制登录,您还可以使用'[Authorize]'属性(而不是处理HTTP结果)。 –
谢谢,我已经做到了。 –