1

I have created an Asp.Net Web Api project and used Individual user accounts. When I am adding users to the table the default system automatically checks if the email address supplied already exists in the table, if so a bad request is thrown otherwise the user can be submitted.

How can I also check if the Phone Number is unique and hasn't already been submitted to the table?

// POST api/Account/Register
        [AllowAnonymous]
        [Route("Register")]
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            **using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var foundPhoneNumber = await db.Users.FirstOrDefaultAsync(x => x.PhoneNumber.Equals(model.PhoneNumber));

                if (foundPhoneNumber != null)
                {
                    return BadRequest("Phone number already exists");
                }
            }**

            var user = new ApplicationUser()
            {
                UserName = model.Email,
                Email = model.Email,
                PhoneNumber = model.PhoneNumber,
                FirstName = model.FirstName,
                LastName = model.LastName,
                MemberNumber = model.MemberNumber,
                CarReg = model.CarReg
            };

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            return Ok();
        }

I have queried the database to check if there is a Phone Number with the same number. This works, but is there a better way to do this?

9
  • What if two people share the same phone number? You know that's a realistic scenario, right? Commented Sep 16, 2019 at 18:28
  • Yes, but in this scenario and example, I want to validate against that. For example, what if I had another number such as an enrollment number that must be unique. Is there a better way or more obvious way that I don't know about to perform the check-in ASP.NET Web Api Commented Sep 16, 2019 at 18:31
  • I think what you're doing is fine. But if you want a more generic way refer to this question for a handy extension method stackoverflow.com/a/31162909/5431968 Commented Sep 16, 2019 at 18:34
  • Possible duplicate of Entity Framework Add if not exist without update Commented Sep 16, 2019 at 18:36
  • I'm not trying to add something to the database. A user enters there details in a form if detail X already exists then the data isn't submitted and they are told on the form. Commented Sep 16, 2019 at 18:39

3 Answers 3

3

Modify your ApplicationUser call and add the following attributes.

public class ApplicationUser : IdentityUser
{
    [MaxLength(17)]
    [IsUnique]
    public string PhoneNumber { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this isn't going to work, phone numbers are notorious hard to check, they can include country codes, spaces, dashes etc. 07564111111 can also be 07564 111111 or 07564 111 111 or 07564-111-111 or +4407564111111 and I can go on like this forever
1

You can override ValidateEntity method in ApplicationUserDbContext class, it will trigger on SaveChanges method.

protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
        {
            if (entityEntry != null && entityEntry.State == EntityState.Added)
            {
                var errors = new List<DbValidationError>();
                ////User Validation
                if (entityEntry.Entity is ApplicationUser user)
                {
                    if (this.Users.Any(u => string.Equals(u.PhoneNumber, user.PhoneNumber)))
                    {
                        errors.Add(new DbValidationError("User",
                          string.Format($"Phonenumber {user.PhoneNumber} is already taken")));
                    }
                }

                if (errors.Any())
                {
                    return new DbEntityValidationResult(entityEntry, errors);
                }
            }
            return new DbEntityValidationResult(entityEntry, new List<DbValidationError>());
        }

Comments

0

Validation can be added via a custom ValidationAttribute that you add to the PhoneNumber property on you model. Here is a simple example:

   [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
   public class NotABananaAttribute : ValidationAttribute
   {
    public override bool IsValid(object value)
    {
        var inputValue = value as string;
        var isValid = true;

        if (!string.IsNullOrEmpty(inputValue))
        {
            isValid = inputValue.ToUpperInvariant() != "BANANA";
        }

        return isValid;
    }
   }

And its used liked this...

public class Model
{
    [NotABanana(ErrorMessage = "Bananas are not allowed.")]
    public string FavoriteFruit { get; set; }
}

Example sourced from: https://riptutorial.com/csharp/example/18486/creating-a-custom-validation-attribute

1 Comment

this is getting closer, but still not enough for phone number comparison

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.