简体   繁体   English

FormsAuthentication重定向到登录页面

[英]FormsAuthentication redirecting to login page

I'm trying to implement Simple FormsAuthentication in an ASP.NET MVC5 application as: 我试图在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 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>

Now when I supply correct credentials as mentioned in Web.Config file, error message from the Else statement part of PostHttp login ViewResult executes and writes message Invalid username or password. Try again 现在,当我提供Web.Config文件中提到的正确凭据时,来自PostHttp login ViewResultElse语句部分的错误消息将执行并写入消息“ Invalid username or password. Try again Invalid username or password. Try again and redirects to login page again. Invalid username or password. Try again重定向到登录页面。 URL shown in the browser is: 浏览器中显示的URL是:

http://payroll.net:81/Home/login?ReturnUrl=%2f

Any idea why FormsAuthentication is not redirecting to "Index" View after receiving correct credentials? 知道为什么收到正确的凭据后FormsAuthentication不会重定向到“索引”视图吗?

You are Supplying it with the unhashed Password , The Default Hashing algorithm is Sha1 try Hashing your Password And put the hashed string. 您正在为其提供未哈希的密码,默认哈希算法是Sha1尝试哈希您的密码并放入哈希字符串。 Also if you do not want to use Encryption you can state it with : 另外,如果您不想使用加密,也可以使用以下命令进行声明:

 <credentials passwordFormat = "Clear">   
     <user name="UserName" 
     password="SimplePass"/>
  </credentials>   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM