C# 401即使在FormsAuthentication.SetAuthCookie()之后也未经授权
我正在尝试用基本表单身份验证实现ASP.NET MVC5应用程序。 这就是我的登录方法:C# 401即使在FormsAuthentication.SetAuthCookie()之后也未经授权,c#,.net,asp.net-mvc,asp.net-mvc-5,forms-authentication,C#,.net,Asp.net Mvc,Asp.net Mvc 5,Forms Authentication,我正在尝试用基本表单身份验证实现ASP.NET MVC5应用程序。 这就是我的登录方法: [HttpPost] [AllowAnonymous] public ActionResult Login(string email, string password, bool? rememberMe) { if (email.IsNullOrWhiteSpace() || password.IsNullOrWhiteSpace()) {
[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");
}
但是在这个重定向方法之后,它给出了一个错误。
同样,HttpContext.User.Identity.IsAuthenticated也是假的
你有什么想法/建议为什么会这样以及如何解决
UPD:我还有一行来自auth的webconfig概念,所以这不是原因
地点
中的web.config文件,以便触发其效果。我找到了原因。出于某种原因,在我的网络配置中有一行,它抑制了表单身份验证
模块
因此,这一行
FormsAuthentication
不起作用,但如果我们将其注释掉或删除,所有操作都会按预期进行。这是因为MVC5默认情况下不使用FormsAuthentication。它使用ASP.NET标识,因此会删除默认模板中的FormsAuthentication。当你替换它时,你没有重新启用FormsAuth。这就是我要找的,所以谢谢你!