gpt4 book ai didi

c# - 401 未经授权,即使在 FormsAuthentication.SetAuthCookie() 之后

转载 作者:行者123 更新时间:2023-11-30 13:56:28 27 4
gpt4 key购买 nike

我正在尝试使用基本表单例份验证实现 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>

最佳答案

我找到了原因。出于某种原因,在我的 webconfig 中有一行抑制了 FormsAuthentication模块

<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>

所以用这条线 FormsAuthentiction不工作,但如果我们将其注释掉或删除,一切都会按预期工作。

关于c# - 401 未经授权,即使在 FormsAuthentication.SetAuthCookie() 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28059448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com