【问题标题】:ASP .Net: AspNetSqlMembershipProvider "unique email" problemASP .Net:AspNetSqlMembershipProvider“唯一电子邮件”问题
【发布时间】:2010-09-23 03:02:28
【问题描述】:

我在我的 ASP .Net 4 Web 应用程序项目中使用 AspNetSqlMembershipProvider。

我已在我的 web.config 文件中将用户地址配置为唯一的(requiresUniqueEmail="true"),如下所示:

<membership>
    <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" 
             type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
             connectionStringName="MyAuthDB" 
             enablePasswordRetrieval="false" 
             enablePasswordReset="true" 
             requiresQuestionAndAnswer="true" 
             applicationName="/" 
             requiresUniqueEmail="true"
             minRequiredPasswordLength="6" 
             minRequiredNonalphanumericCharacters="1" 
             passwordFormat="Hashed" 
             maxInvalidPasswordAttempts="5" 
             passwordAttemptWindow="10" />
  </providers>
</membership>

但是,当我使用数据库中已有的电子邮件执行以下代码时,虽然没有将新行添加到 aspnet_Membership 表中,但正在向:aspnet_Users 和 aspnet_Profile 表中添加一个条目。

有什么方法可以阻止这些条目也被添加到上述两个表中?

下面是代码后面的代码:

if (Membership.GetUser(EN(this.Id.Value)) != null) {
    this.CustomFieldValidatorId.IsValid = false;
}
else {
    try {
        string username = EN(this.Id.Value);
        string password = EN(this.Password.Value);
        string email = EN(this.Email.Value);
        string question = EN(this.SecurityQuestion.Value);
        string answer = EN(this.Answer.Value);

        string firstname = EN(this.FirstName.Value);
        string lastname = EN(this.LastName.Value);
        DateTime birthdate = new DateTime(
            Convert.ToInt32(EN(this.BirthYear.SelectedValue)),
            Convert.ToInt32(EN(this.BirthMonth.SelectedValue)),
            Convert.ToInt32(EN(this.BirthDay.SelectedValue)));
        string company = EN(this.Company.Value);
        string add1 = EN(this.StreetAddress1.Value);
        string add2 = EN(this.StreetAddress2.Value);
        string city = EN(this.City.Value);
        string state = EN(this.State.Value);
        string zip = EN(this.Zip.Value);
        string country = EN(this.Country.SelectedValue);
        string countrycode = EN(this.CountryCode.Value);
        string areacode = EN(this.AreaCode.Value);
        string phonenum = EN(this.PhoneNumber.Value);
        string extension = EN(this.Extension.Value);

        MembershipCreateStatus S;
        Membership.CreateUser(username, password, email, question, answer, false, out S);

        WebProfile wp = new WebProfile();
        wp.Initialize(username, true);

        wp.PersonalInformation.FirstName = firstname;
        wp.PersonalInformation.LastName = lastname;
        wp.PersonalInformation.BirthDate = birthdate;
        wp.PersonalInformation.Company = company;
        wp.PersonalInformation.StreetAddress1 = add1;
        wp.PersonalInformation.StreetAddress2 = add2;
        wp.PersonalInformation.City = city;
        wp.PersonalInformation.State = state;
        wp.PersonalInformation.Zip = zip;
        wp.PersonalInformation.Country = country;
        wp.PersonalInformation.PhoneCountryCode = countrycode;
        wp.PersonalInformation.PhoneAreaCode = areacode;
        wp.PersonalInformation.PhoneNumber = phonenum;
        wp.PersonalInformation.PhoneExtension = extension;

        wp.Save();

        MembershipUser user = Membership.GetUser(username);
        Roles.AddUserToRole(username, "Developer");
        Membership.UpdateUser(user);

        EmailDeveloper(firstname, lastname, email, (Guid)user.ProviderUserKey);

        this.DeveloperEmail.Text = email;
    }
    catch (MembershipCreateUserException ex) {
        switch (ex.StatusCode) {
            case MembershipCreateStatus.DuplicateEmail:
                this.CustomFieldValidatorEmail.IsValid = false;
                break;
            default:
                this.CustomFieldValidatorGeneral.ErrorMessage = ex.Message.ToString();
                this.CustomFieldValidatorGeneral.IsValid = false;
                break;
        }
    }
}

private string EN(string v) {
    return HttpUtility.HtmlEncode(v.Trim());
}

【问题讨论】:

  • Membership.CreateUser(...) 是否返回一个布尔值来指示它是否有效?如果是这样,请检查。

标签: c# asp.net sqlmembershipprovider


【解决方案1】:

您只需在尝试创建用户后检查MembershipCreateStatus S; 的值,而不是通过您的配置文件创建代码。

这就是它的目的。

例如

MembershipCreateStatus S;
Membership.CreateUser(username, password, email, question, answer, false, out S);

if(S != MembershipCreateStatus.Success)
{
   // throw exception or display message and exit here
   // DO NOT PASS GO, DO NOT COLLECT $2000 (adjusted for inflation) 
   // and in NO circumstances fall through to the code below that creates
   // the profile and aspnet_users placeholder record that you mention
}

参考:

public enum MembershipCreateStatus
{
    Success,
    InvalidUserName,
    InvalidPassword,
    InvalidQuestion,
    InvalidAnswer,
    InvalidEmail,
    DuplicateUserName,
    DuplicateEmail,
    UserRejected,
    InvalidProviderUserKey,
    DuplicateProviderUserKey,
    ProviderError
}

【讨论】:

  • 我认为 try-catch 块会将其捕获为异常
  • 提供程序方法通常不会抛出异常。在这种情况下,它没有。这就是 out Membership 状态的用途
猜你喜欢
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 2015-03-02
  • 2021-08-22
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多