2010-09-23 84 views
3

我在ASP.Net 4 Web應用程序項目中使用AspNetSqlMembershipProvider。ASP .Net:AspNetSqlMembershipProvider「唯一電子郵件」問題

我已經配置的用戶地址是唯一的(requiresUniqueEmail =「真」),在下面我的web.config文件:

<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()); 
} 
+0

是否'Membership.CreateUser(...)'返回一個布爾值指示是否它的工作?如果是這樣,請檢查。 – 2010-09-23 04:48:34

回答

5

你只需要檢查的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 
} 
+0

我認爲try-catch塊會捕獲異常 – Moon 2010-09-23 05:17:08

+0

提供者方法通常不會引發異常。在這種情況下,它不會。這是出於成員資格狀態 – 2010-09-23 05:24:33