0

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

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而失败,这是我所期望的。

知道为什么吗?

这是 config 中的成员资格部分(如果有影响的话),尽管我不知道如何:

    <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>

提前致谢!

4

1 回答 1

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。

于 2012-11-02T19:09:29.177 回答