gpt4 book ai didi

asp.net-mvc - Request.IsAuthenticated 不会更改 ASP.NET MVC 中 FormsAuthentication 中的 View

转载 作者:行者123 更新时间:2023-12-03 23:15:56 25 4
gpt4 key购买 nike

我正在开发一个简单的 asp.Net MVC 应用程序,它需要 FormsAuthentication,

模型

public class Member
{
[Required]
[Display(Name = "Username")]
public string Username { set; get; }

[Required]
[Display(Name = "Password")]
public string Password { set; get; }

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

public bool IsValid(string username,string password)
{
return (new TestdbEntities()).Members.Any(m => m.Username == username && m.Password == password);
}
}

Controller

[HttpGet]
public ActionResult Login()
{
return View();
}

[HttpPost]
public ActionResult Login(Models.Member member)
{
if (ModelState.IsValid)
{
if (member.IsValid(member.Username, member.Password))
{
FormsAuthentication.SetAuthCookie(member.Username,member.RemeberMe);
return RedirectToAction("Index","Home");
}
else
{
ModelState.AddModelError("","Invalid Username/Passowrd!");
}
}
return View(member);
}

查看

问题发生在 View 中,当用户成功通过身份验证时,我希望登录链接更改为注销链接,但即使我跟踪,登录成功但 Request.IsAuthenticated 为 false。

<body>
<ul class="nav nav-pills">
<li>
@Html.ActionLink("Home", "Index", "Home")
</li>
<li>
@if (Request.IsAuthenticated)
{
<label>Welcome </label> @Html.Encode(User.Identity.Name)
@Html.ActionLink("Signout", "Logout", "Membership")
@Html.Label(User.Identity.Name.ToString())
}
else
{
@Html.ActionLink("Login", "Login", "Membership")
}
</li>
</ul>
<div>
@RenderBody()
</div>
</body>

最佳答案

检查您的 web.config 文件,您必须添加 <authentication mode="Forms"/><system.web> 下标签

关于asp.net-mvc - Request.IsAuthenticated 不会更改 ASP.NET MVC 中 FormsAuthentication 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097601/

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