【发布时间】:2015-03-19 12:19:53
【问题描述】:
我正在尝试使用基本表单身份验证实现 ASP.NET MVC5 应用程序。 于是就有了我的登录方式:
[HttpPost]
[AllowAnonymous]
public ActionResult Login(string email, string password, bool? rememberMe)
{
if (email.IsNullOrWhiteSpace() || password.IsNullOrWhiteSpace())
{
return RedirectToAction("Index");
}
UserEntity user = new UserEntity() {Email = email, PasswordHash = password};
var userFromDb = _userService.FindUser(user);
if (userFromDb != null)
{
FormsAuthentication.SetAuthCookie(userFromDb.Name, rememberMe.GetValueOrDefault());
var a = HttpContext.User.Identity.IsAuthenticated; //This is still false for some reson
return RedirectToAction("Index", "User");
}
return RedirectToAction("Index");
}
但是在重定向这个方法之后它给了我401 Unauthorized 错误。
也好像 HttpContext.User.Identity.IsAuthenticated 是假的。
您对为什么会这样以及如何解决有什么想法/建议吗?
UPD:我也有一个来自 auth 的 webconfig 概念,所以这不是原因
<authentication mode="Forms">
<forms loginUrl="~/Login/Index" />
</authentication>
【问题讨论】:
标签: c# .net asp.net-mvc asp.net-mvc-5 forms-authentication