为什么(有时)Membership.ValidateUser首先调用PasswordFormat以及如何?

时间:2018-05-22 11:29:19

标签: c# .net asp.net-membership

我在计算机上运行MVC项目。我已在网络配置中设置了自定义成员资格,例如:

<membership defaultProvider="CustomMembership">
  <providers>
    <clear/>
    <add name="CustomMembership" type="GotPayments.CustomAuthentication.CustomMembership"/>
  </providers>
</membership>

我已经实现了一些我需要的功能;对于其他人,我只是扔了NotImplementedException,因为我没有使用它。因此,当我调用已实现的ValidateUser用户时,一切正常:

public override bool ValidateUser(string email, string password)
{
    if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
    {
        return false;
    }

    using (MyProject ctx = new MyProject())
    {
        var user = (from us in ctx.Users
                    where string.Compare(email, us.Email, StringComparison.OrdinalIgnoreCase) == 0
                    && string.Compare(password, us.Password, StringComparison.OrdinalIgnoreCase) == 0
                    && us.IsActive == true
                    select us).FirstOrDefault();

        return (user != null) ? true : false;
    }
}   

但是,一旦我在另一台机器上复制项目,并且我运行它,相同的功能(在执行之前;所以F11在调试时不要进入ValidateUser)调用:

public override MembershipPasswordFormat PasswordFormat
{
    get
    {
        throw new NotImplementedException();
    }
}

抛出异常。

为什么只打电话给它&#34;有时&#34;?框架如何称呼它? 我自己从不称呼这个功能。

堆栈跟踪:

in System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)  
in System.Environment.get_StackTrace() 
in MyProject.CustomAuthentication.CustomMembership.get_PasswordFormat() in CustomMembership.cs:riga 117
in System.Web.Security.Membership.CheckedPasswordFormat(MembershipProviderCollection providers)
in System.Web.Security.Membership.Initialize()
in System.Web.Security.Membership.get_Provider()
in System.Web.Security.Membership.ValidateUser(String username, String password)
in MyProject.Controllers.AccountController.Login(LoginViewModel loginView) in AccountController.cs:riga 27

Login.cs

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel loginView)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(loginView.Email, loginView.Password))
        {
            // ...
        }
    }

    // ...
}

0 个答案:

没有答案