Reputation: 1972
We are working on an application which uses ASP.NET Identity (v 1.0). We defined an User
class that implements the IUser
interface. Our repository class UserRepository
implements IUserStore
.
Now when I call UserManager.CreateAsync(user, password)
it calls FindByNameAsync()
method of UserRepository
to check (I guess) if this new user has an unique ID.
However I don't understand why it calls this method instead of FindByIdAsync
?
I supposed that the UserName
property on the IUser
interface is something like person name (e.g. "John Smith") and Id
is an internal unique identifier of an user.
From this default implementation I guess I was wrong.
The question is: why do we need these two properties (Id
and UserName
) on the IUser
interface in this case? What the difference between them? Which of them is supposed to be an unique ID?
Upvotes: 2
Views: 1029
Reputation: 93444
ID's are typically numbers, although the default implementation of Identity uses Guid's. Name is username, typically a handle or shortening of a full name. Often times this is an email address, depends on how you set it up.
Id's are used as primary keys in your database. Names are not. The reason is that you might delete a user with a given username, and another use might come along and want to create an account with the same name. But, you still need a way to identify that these are two different users for auditing and historical reasons.
When creating a user, how exactly would the code know the userid to look it up? The username is the only thing it knows...
Upvotes: 4
Reputation: 5908
They're both unique. Createasync calls findbyname as you're not going to know an Id for a user that you're going to create as it doesn't exist yet.
FindByID Exists as it's quicker to do a lookup on that after you've created a user.
Upvotes: 1