Alexander Polyankin
Alexander Polyankin

Reputation: 1887

Asp.Net Identity 2 duplicate username check

UserManager in Asp.Net Identity 2 prevents creation user with duplicate username through additional request to database to find possible duplicate. I think this is error prone and can cause concurrency errors. The correct mechanism should rely on on unique constraints or indexes. Am I wrong and do I miss something?

Links to source: CreateAsync and ValidateUserName

Upvotes: 1

Views: 3817

Answers (1)

trailmax
trailmax

Reputation: 35126

No, you are not wrong. And Identity adds the unique index on Username column:

Tables generated by Identity

And the migration code for this table is:

        CreateTable(
            "dbo.AspNetUsers",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                      /*  .... SNIP .... */
                    UserName = c.String(nullable: false, maxLength: 256),
                })
            .PrimaryKey(t => t.Id)
            .Index(t => t.UserName, unique: true, name: "UserNameIndex");

Unique index is clearly set on the column.

p.s. you are looking on Identity v3 - it is not released. Current Identity v2.1 is not open source yet.

Upvotes: 3

Related Questions