-1

这是我的登录控制器:

[HttpPost]
public ActionResult Login(LoginViewModel login, string ReturnUrl = "/Admin/pages/index")
{
    if (ModelState.IsValid)
    {
        if (loginRepository.IsUserExist(login.UserName, login.Password))
        {
            FormsAuthentication.SetAuthCookie(login.UserName, login.RememberMe);
            return Redirect(ReturnUrl);
        }
        else
        {
            ModelState.AddModelError("UserName", "Username and password is incorrect");
        }
    }
    return View();
}

我将 cookie 设置为 Admin Area 后将其重定向,但它给了我错误:

“HTTP 错误 401.0 - 未经授权”

我的管理区域控制器:

[Authorize]
public class PagesController : Controller
{
    public ActionResult Index()
    {
        return View(pageRepository.GetAllPage());
    }
}
4

1 回答 1

0

我发现问题出在 web.config

必须在 web.config 中添加以下行:

<authentication mode="Forms">
  <forms name="Website" loginUrl="/Account/login" timeout="43200"></forms>
</authentication>
于 2021-01-12T08:37:22.890 回答