Membership.CreateUser应该失败但不会失败

时间:2012-11-02 10:30:23

标签: asp.net membership

我正在创建用于测试目的的用户,使用:

string username = ...
string password = ...
string email = "******** not a valid email address! *********";
MembershipUser NewUser = Membership.CreateUser(userName, password, email, "no question", "no answer", true, out createStatus);
if (NewUser == null)
{
  switch (createStatus)
  {
    case MembershipCreateStatus.DuplicateUserName:
      throw new Exception("There already exists a user with this username.");
    case MembershipCreateStatus.InvalidEmail:
      throw new Exception("There email address you provided in invalid.");
    case MembershipCreateStatus.InvalidPassword:
      throw new Exception("The password you provided is invalid. It must be seven characters long.");
    default:
      throw new Exception("There was an unknown error; the user account was NOT created.");
  }
}

当执行此操作时,将创建一个新用户,它不会因NewUser == null, MembershipCreateStatus.InvalidEmail 而失败,这正是我所期望的。

知道为什么吗?

这是配置中的成员资格部分,如果有一个方面,虽然我没有看到:

    <membership defaultProvider="myProvider">
  <providers>
    <add
            name="myProvider"
            applicationName="/"
            connectionStringName="myconnectionsString"
            enablePasswordRetrieval="true"
            enablePasswordReset="true"
            passwordFormat="Clear"
            requiresQuestionAndAnswer="false"
            requiresUniqueEmail="true"
            minRequiredNonalphanumericCharacters="0"
            type="System.Web.Security.SqlMembershipProvider"
            />
  </providers>
</membership>

提前致谢!

1 个答案:

答案 0 :(得分:0)

来自反射器,这是SqlMembershipProvider

中电子邮件的验证
  if (!SecUtility.ValidateParameter(ref email, this.RequiresUniqueEmail, this.RequiresUniqueEmail, false, 0x100))
    {
        status = MembershipCreateStatus.InvalidEmail;
        return null;
    }

internal static bool ValidateParameter(ref string param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize)
{
    if (param == null)
    {
        return !checkForNull;
    }
    param = param.Trim();
    return (((!checkIfEmpty || (param.Length >= 1)) && ((maxSize <= 0) || (param.Length <= maxSize))) && (!checkForCommas || !param.Contains(",")));
}

看起来它并不关心是否有效的电子邮件,只是提供了一些东西。您需要从客户端处理它或覆盖SqlMembershipProvider。