我正在尝试在 ASP.NET
MVC5 应用程序中实现 Simple FormsAuthentication
,如下所示:
Controller
public ViewResult login()
{
return View();
}
[HttpPost]
public ViewResult login(Login login)
{
if (ModelState.IsValid)
{
if (FormsAuthentication.Authenticate(login.username, login.password))
{
FormsAuthentication.RedirectFromLoginPage(login.username, false);
}
else
{
Response.Write("Invalid username or password. Try again");
return View();
}
}
return View("Index");
}
Web.Config
<appSettings>
<add key="owin:AutomaticAppStartup" value="false"/>
</appSettings>
<system.web>
<authentication mode="Forms">
<forms
name="PC"
loginUrl="~/Home/login"
timeout="2">
<credentials>
<user name="admin" password="admin1"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
现在,当我提供 Web.Config
文件中提到的正确凭据时,PostHttp login ViewResult
的 Else
语句部分中的错误消息将执行并写入消息
用户名或密码无效。再试一次
并再次重定向到登录页面。浏览器中显示的 URL 为:
http://payroll.net:81/Home/login?ReturnUrl=%2f
知道为什么 FormsAuthentication 在收到正确的凭据后没有重定向到“索引” View 吗?
最佳答案
您正在为其提供未散列的密码,默认散列算法是 Sha1 尝试散列您的密码并放入散列字符串。 另外,如果您不想使用加密,您可以使用以下方式声明:
<credentials passwordFormat = "Clear">
<user name="UserName"
password="SimplePass"/>
</credentials>
关于c# - FormsAuthentication 重定向到登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34529349/