ASP.NET的:AspNetSqlMembershipProvider"唯一的电子邮件"问题 [英] ASP .Net: AspNetSqlMembershipProvider "unique email" problem

查看:214
本文介绍了ASP.NET的:AspNetSqlMembershipProvider"唯一的电子邮件"问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ASP .NET 4.0 Web应用程序项目使用AspNetSqlMembershipProvider。

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

 <会员和GT;
    <供应商>
        <清/>
        <添加名称=AspNetSqlMembershipProvider
             键入=System.Web.Security.SqlMembershipProvider,System.Web程序,版本= 2.0.0.0,文化=中性公钥= b03f5f7f11d50a3a
             的connectionStringName =MyAuthDB
             enablePasswordRetrieval =假
             enablePasswordReset设置=真
             requiresQuestionAndAnswer =真
             的applicationName =/
             requiresUniqueEmail =真
             minRequiredPasswordLength =6
             minRequiredNonalphanumericCharacters =1
             了passwordFormat =散列
             maxInvalidPasswordAttempts =5
             passwordAttemptWindow =10/>
  < /供应商>
< /会员>

但是,当我在数据库中与电子邮件执行以下code已经,虽然新的行没有被添加到aspnet_Membership表,条目被添加到:aspnet_Users和aspnet_Profile表。

有什么办法,我可以将其添加到上面呢?

描述的两个表停止这些条目

下面是code中的code背后:

 如果(Membership.GetUser(EN(this.Id.Value))!= NULL){
    this.CustomFieldValidatorId.IsValid = FALSE;
}
其他{
    尝试{
        字符串的用户名= EN(this.Id.Value);
        字符串密码= EN(this.Password.Value);
        字符串email = EN(this.Email.Value);
        字符串的问题= EN(this.SecurityQuestion.Value);
        串答案= EN(this.Answer.Value);        字符串名字= EN(this.FirstName.Value);
        字符串姓氏= EN(this.LastName.Value);
        DateTime的生日=新日期时间(
            Convert.ToInt32(EN(this.BirthYear.SelectedValue))
            Convert.ToInt32(EN(this.BirthMonth.SelectedValue))
            Convert.ToInt32(EN(this.BirthDay.SelectedValue)));
        串公司= EN(this.Company.Value);
        字符串ADD1 = EN(this.StreetAddress1.Value);
        字符串ADD2 = EN(this.StreetAddress2.Value);
        串城市= EN(this.City.Value);
        字符串状态= EN(this.State.Value);
        串邮编= EN(this.Zip.Value);
        字符串国家= EN(this.Country.SelectedValue);
        字符串国code = EN(this.Country code.Value);
        弦面积code = EN(this.Area code.Value);
        字符串PHONENUM = EN(this.PhoneNumber.Value);
        串延长= EN(this.Extension.Value);        MembershipCreateStatus S;
        Membership.CreateUser(用户名,密码,电子邮件,提问,回答,假的,出S);        WebProfile WP =新WebProfile();
        wp.​​Initialize(用户名,真实);        wp.​​PersonalInformation.FirstName =名字;
        wp.​​PersonalInformation.LastName =姓氏;
        wp.​​PersonalInformation.BirthDate =生日;
        wp.​​PersonalInformation.Company =公司;
        wp.​​PersonalInformation.StreetAddress1 = ADD1;
        wp.​​PersonalInformation.StreetAddress2 = ADD2;
        wp.​​PersonalInformation.City =城市;
        wp.​​PersonalInformation.State =状态;
        wp.​​PersonalInformation.Zip =拉链;
        wp.​​PersonalInformation.Country =国家;
        wp.​​PersonalInformation.PhoneCountry code =国家code;
        wp.​​PersonalInformation.PhoneArea code =区域code;
        wp.​​PersonalInformation.PhoneNumber = PHONENUM;
        wp.​​PersonalInformation.PhoneExtension =延伸;        wp.​​Save();        用户的MembershipUser = Membership.GetUser(用户名);
        Roles.AddUserToRole(用户名,开发);
        Membership.UpdateUser(用户);        EmailDeveloper(名字,姓氏,电子邮件(GUID)user.ProviderUserKey);        this.DeveloperEmail.Text =电子邮件;
    }
    赶上(MembershipCreateUserException前){
        开关(ex.Status code){
            案例MembershipCreateStatus.DuplicateEmail:
                this.CustomFieldValidatorEmail.IsValid = FALSE;
                打破;
            默认:
                this.CustomFieldValidatorGeneral.ErrorMessage = ex.Message.ToString();
                this.CustomFieldValidatorGeneral.IsValid = FALSE;
                打破;
        }
    }
}私人字符串EN(串V){
    返回HttpUtility.HtmlEn code(v.Trim());
}


解决方案

您只需要检查 MembershipCreateStatus S的值; 后试图创建用户,而不是通过下降到您的配置文件创建code。

这就是它是有什么。

例如

  MembershipCreateStatus S;
Membership.CreateUser(用户名,密码,电子邮件,提问,回答,假的,出S);如果(S!= MembershipCreateStatus.Success)
{
   //抛出异常或在这里显示信息并退出
   //不通过GO,不收$ 2000(经通胀调整)
   //并在任何情况下告吹下面的code创建
   //配置文件并aspnet_users占位符,你提到的记录
}

参考:

 公开枚举MembershipCreateStatus
{
    成功,
    无效的用户名,
    无效的密码,
    InvalidQuestion,
    InvalidAnswer,
    不合规电邮,
    DuplicateUserName,
    DuplicateEmail,
    UserRejected,
    InvalidProviderUserKey,
    DuplicateProviderUserKey,
    ProviderError
}

I am using AspNetSqlMembershipProvider in my ASP .Net 4 web application project.

I have configured user address to be unique (requiresUniqueEmail="true") in my web.config file as below:

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

But, when I execute the following code with an email already in the database, although new row is not being added to aspnet_Membership table, an entry is being added to: aspnet_Users and aspnet_Profile tables.

Is there any way I can stop these entries from being added to the two tables described above as well?

Here is the code from code behind:

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

解决方案

You simply need to check the value of MembershipCreateStatus S; after you attempt to create the user and not fall through to your profile creation code.

That is what it is there for.

e.g.

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
}

Reference:

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

这篇关于ASP.NET的:AspNetSqlMembershipProvider&QUOT;唯一的电子邮件&QUOT;问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆