2

I'm developing a WEB API asp.net project with visual studio 2013, and I'm implementing my custom Identity model for custom registration. For example I've added an email field in the RegisterBindingModel.

This is what I've done: How to extend asp.net web api 2 user?

I've added email property, and I need that property to be unique (as UserName) , but by default that email property can be repeated. My question is how do I make this email property unique, like UserName? I've read that unique properties can be set with UserValidator, but can't find how. Can you tell me how I can make that, with UserValidator or the best way?

And how's the UserName property defined as unique? I can't find it anywhere.

Thank you.

Community
  • 1
  • 1
Sid
  • 55
  • 6
  • I don't understand - do you want to use the users email address as their username, or do you want an additional property against the user to hold their email address? Why not just make the username the email address? – Brendan Green Mar 07 '14 at 04:02
  • Because I need username property for username, and email property for email, and instead I need to know how make unique properties; that's the main question. Because who know what other fields I would need in the future to be set as unique. Thanks for the suggestion btw – Sid Mar 07 '14 at 04:05
  • Add a unique constraint to the ApplicationUser table. How you do this will depend on what EF migration strategy you are using. – Brendan Green Mar 07 '14 at 04:18
  • Yep, I know I can do it on db level, but I need to do it trought Asp.net Identity. Done before at db level but I dont like that solution mainly because I have to process the error by code; but if I process with Asp.net Identity the error will store in model. UserName has not unique constraint and is set as unique is that why I ask how's that made; and that why I want make it this way, because I know it can be done trought Asp.net Identity; but the thing is that dont know how. – Sid Mar 07 '14 at 04:25
  • 1
    Are you using a membership provider?http://stackoverflow.com/questions/3775182/asp-net-aspnetsqlmembershipprovider-unique-email-problem – Chad McGrath Mar 07 '14 at 05:25
  • Nice solution. But what if I need more properties , telephone number or idk, any desired unique property from the model. There's any way this solution could work? – Sid Mar 07 '14 at 06:29

1 Answers1

3

There a different ways to implement this, but not out of the box. However, you can implement an extensible validation using Fluent Validation

[Validator(typeof(ApplicationUserValidator))]
class ApplicationUser : IdentityUser
{
    public string Email { get; set; }
    //...Model implementation omitted
}

public class ApplicationUserValidator : AbstractValidator<ApplicationUser>
{
    public ApplicationUserValidator()
    {

        RuleFor(x => x.Email).Must(IsUniqueEmail).WithMessage("Email already exists");
    }
    private bool IsUniqueEmail(string mail)
    {
        var _db = new DataContext();
        if (_db.NewModel.SingleOrDefault(x => x.Email == mail) == null) return true;
        return false;
    }
    //implementing additional validation for other properties:
    private bool IsUniqueTelephoneNumber(string number)
    {
      //...implement here
    }
}
Marco
  • 22,856
  • 9
  • 75
  • 124
  • Didn't test this solution but I think this is what i've needed. Thanks for the proposed solution by now I think is the best can adapt to my problem, but there's a thing I don't like, it has a dependency library. How could do the same without library do you know any examples or where to start from there? Thank you so much. – Sid Mar 08 '14 at 03:07
  • 1
    Check the accepted answer here: http://stackoverflow.com/questions/21903355/compare-email-address-entered-to-database-with-dataannotations/21903709#21903709. I personally prefer FluentValidation, because I use it regularly. – Marco Mar 08 '14 at 08:48