I am doing examples from APRESS ASP MVC Book. And I get to membership. I follow example from the book, but I want to change to not have credentials in web.config. But I always get 'false' from 'FormsAuthentication.Authenticate()'
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<membership defaultProvider="MyMembershipProvider">
<providers>
<add name="MyMembershipProvider"
connectionStringName="myConnectionString"
applicationName="MyMembership"
开发者_运维百科 enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
type="VVU.CityLink.WebUI.Infrastructure.Concrete.FormsAuthProvider"
minRequiredNonalphanumericCharacters="0"/>
</providers>
</membership>
<authorization>
<deny users="?"></deny>
</authorization>
[HttpPost]
public ActionResult LogOn(LogOnViewModel model, string returnUrl)
{
if(ModelState.IsValid)
{
if (authProvider.Authenticate(model.Username, model.Password))
{
return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
}
else
{
ModelState.AddModelError("", "Incorrect username or password");
return View();
}
}
else
{
return View();
}
}
public class FormsAuthProvider : IAuthProvider
{
public bool Authenticate(string username, string password)
{
bool result = FormsAuthentication.Authenticate(username, password);
if(result)
{
FormsAuthentication.SetAuthCookie(username, false);
}
return result;
}
}
精彩评论