gpt4 book ai didi

c# - AuthorizeAttribute 不断重定向到/Account/Login

转载 作者:太空狗 更新时间:2023-10-29 23:22:08 25 4
gpt4 key购买 nike

我正在努力研究 ASP.NET MVC 中的表单例份验证。 MVC 5 在我的具体情况下,以防万一。

我的应用程序不使用密码,只使​​用电子邮件地址作为用户名。

在调试 Login 方法时,我可以清楚地看到模型是有效的,并且我的(自定义)MembershipProvider 按预期验证了用户。
然后它重定向到提供的 returnUrl(出于测试目的,我在/Home/About 上有一个 AuthorizeAttribute)。

遗憾的是,我立即返回到 Login View ,所以很明显我缺少整个过程的基本元素(并且,通过扩展,对整个 auth/auth 过程的基本洞察,我必须承认,因为我很少玩它)。

登录方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if(ModelState.IsValid && Membership.ValidateUser(model.Email, ""))
{
FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);

if (Url.IsLocalUrl(returnUrl))
{
return RedirectToLocal(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "Email address unknown");
}

return View(model);
}

登录 View 模型:

public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }

[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}

Web.config 的相关部分:

<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
</system.web>

我没看到什么?我应该看哪里?

最佳答案

您正在使用 FormsAuthentication 设置您的 cookie。如果您使用的是 MVC5,他们会使用 [Authorize] 属性删除该类型的身份验证。

在您的 web.config 中寻找它。如果您想使用 FormsAuthentication,请删除该行。

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

您可能想阅读有关 Microsoft 为何在 MVC5 中删除 FormsAuthentication 以及如何改用 OWIN 的信息:http://blogs.msdn.com/b/webdev/archive/2013/07/03/understanding-owin-forms-authentication-in-mvc-5.aspx

关于c# - AuthorizeAttribute 不断重定向到/Account/Login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27549248/

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