要解决"Asp Net MVC 5 User Identity的IsAuthenticated属性返回false"的问题,可以按照以下步骤进行操作:
User.Identity.IsAuthenticated
属性来获取用户身份验证状态。确保代码正确访问该属性。public ActionResult MyAction()
{
if (User.Identity.IsAuthenticated)
{
// 用户已通过身份验证
}
else
{
// 用户未通过身份验证
}
// 其他逻辑
return View();
}
FormsAuthenticationTicket
并将其添加到响应的Cookie中。var authTicket = new FormsAuthenticationTicket(
1,
user.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30), // 设置Cookie的有效期
rememberMe, // 是否记住用户
userData); // 可选的用户数据,如用户ID
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(authCookie);
以上是一个通用的解决方法,但具体问题的解决方法可能会因为代码的实现方式有所不同。如果以上方法无法解决问题,请提供更多的代码和错误信息,以便我们更好地帮助您解决问题。