0

我正在开发这个小 MVC 项目,该项目正在成形。刚才我想我会在 Internet Explorer 中测试它,它显示了一些奇怪的行为,我真的不知道在哪里寻找原因/解决方案。

它在这里直播:www.begijnhof.net/CodeBox

在 FireFox、我的 WP7 手机上的 IE、iphone 或 Chrome 上的 safari 中,您注册/登录,它会将您带到主页,然后在页面左上方显示额外的按钮。

在 IE 中,当您提供正确的凭据时(user/user1*),我认为它会进行身份验证,因为它不会显示任何错误,并且会在您提供错误的凭据时进行。但它没有显示按钮等。

在对其进行了更多测试之后,它仍然有效,即使我删除了所有的 cookie 等,但明天左右它很快就不会再工作了。

谁能在这个问题上指出我正确的方向?谢谢..

好的,对不起:我在考虑第二个问题,错误的代码!所以登录过程如下:

[HttpPost]
    public ActionResult LogIn(LogOnViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (members.ValidateUser(model.Username, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.Username, false);
                return RedirectToAction("List", "Snippet");
            }

            //Check to see what's wrong 
            if (userRepo.IsUserLockedOut(model.Username))
                ModelState.AddModelError("", "Authentication failed!");
            if (userRepo.Users.FirstOrDefault(u => u.Username == model.Username) == null)
                ModelState.AddModelError("", "User does not exists!");
            if (!userRepo.IsUserLockedOut(model.Username) && userRepo.Users.FirstOrDefault(u => u.Username == model.Username) != null)
                ModelState.AddModelError("", "The password is not correct!");
            return View(model);
        }
        return View(model);
    } 

编辑:我想我找到了导致它的原因,但没有解决方案。

它所在的域具有隐藏功能,因此 URL 始终是 /CodeBox,这会导致一些问题。

4

1 回答 1

0

我认为问题是这样的:

if (members.ValidateUser(model.Username, model.Password)) 
{ 
    FormsAuthentication.SetAuthCookie(model.Username, false);
    return RedirectToAction("List", "Snippet"); 
}

将第二个参数设置FormsAuthentication.SetAuthCookie为 false 意味着 cookie 不会在浏览器会话中持续存在。这意味着如果您关闭浏览器,cookie 就会被删除。尝试将其设置为 true:

if (members.ValidateUser(model.Username, model.Password)) 
{ 
    FormsAuthentication.SetAuthCookie(model.Username, true);
    return RedirectToAction("List", "Snippet"); 
}
于 2012-08-08T11:33:31.517 回答