问题描述:在ASP.NET MVC中,登录用户仍然跳转到登录页面的解决方法。
解决方法:
FormsAuthentication.SetAuthCookie()方法来设置认证Cookie。例如:[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
// 在此处进行用户名和密码的验证逻辑
// 验证成功后设置认证Cookie
FormsAuthentication.SetAuthCookie(model.Username, false);
return RedirectToAction("Index", "Home");
}
return View(model);
}
web.config文件中,确保节点中的mode属性设置为Forms。例如:
[Authorize]特性。例如:[Authorize]
public class HomeController : Controller
{
// ...
}
AuthenticationManager.SignIn()方法来设置认证Cookie。例如:[HttpPost]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid)
{
// 在此处进行用户名和密码的验证逻辑
// 验证成功后设置认证Cookie
var claimsIdentity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, model.Username) }, "ApplicationCookie");
var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, claimsIdentity);
return RedirectToAction("Index", "Home");
}
return View(model);
}
这些解决方法可以帮助你确保登录用户不再跳转到登录页面。请根据你的具体应用程序需求选择适合的解决方法。