Poe*_*HaH 2 asp.net-mvc forms-authentication
我对.NET和安全性都很陌生.我选择实现Forms身份验证(如果我应该使用其他东西,请更正我).从我在互联网上收集到的内容,我做了以下操作,但它不起作用:
Web.config文件
<authentication mode="Forms">
<forms loginUrl="~/Home/Index" timeout="30" />
</authentication>
Run Code Online (Sandbox Code Playgroud)
HTTPPost ajax登录方法:
[HttpPost]
public ActionResult Login(LoginInputModel loginModel)
{
if (ModelState.IsValid)
{
var success = UserService.Login(loginModel.Password, loginModel.Email);
if (success)
{
return Json(new { Url = Url.Action("Index","Home") });
}
loginModel.ErrorMessages = "Failed to log in with these credentials. Please try again.";
return PartialView("Widgets/Login/_LoginInput", loginModel);
}
return PartialView("Widgets/Login/_LoginInput", loginModel);
}
Run Code Online (Sandbox Code Playgroud)
使用UserService类中的实际登录代码:
public static bool Login(string password, string email)
{
var user = Connector.GetUserByCredentials(password, email);
if (user == null) return false;
FormsAuthentication.SetAuthCookie(email, false); // this line
SessionService.Delete(UserSessionKey);
SessionService.Store(UserSessionKey, UserMapper.DbUserToUser(user));
return SessionService.HasKey(UserSessionKey);
}
Run Code Online (Sandbox Code Playgroud)
每当我点击登录,它都可以正常工作(刷新页面,我看到不同的内容),但如果我导航到另一个页面,我会再次被重定向到登录页面.我(不)做错了什么?
如果您需要更多代码,我会很乐意发布它.
Eri*_*sch 15
当你说你正在使用MVC5时,你使用的是什么版本的Visual Studio?您使用的是最初由默认向导创建的应用程序吗?
如果应用程序是由默认向导创建的,则默认情况下它会启用ASP.NET标识,并从处理中删除FormsAuthentication模块.如果要继续使用FormsAuth,则必须从Forms.config中为FormsAuthentication模块删除"remove"键.
您需要删除此行
<system.webServer>
<modules>
<remove name="FormsAuthentication" /> <----****
</modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5530 次 |
| 最近记录: |