You cant change the base ID of the IdentityUser
using the default membership provider. You will have to roll your own IUser
implementation and storage.
There is a great topic over here How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser that talks about creating your own implementation.
Now that being said (in my personal opinion [stop debate about int
vs uniqueidentifier
for primary key storage and indexing]) the Identity of the UserID (Id
) property is a better unique user ID than an integer. Although the class is implemented as a string its underlying type is based on a Guid
(see appendix 1 at bottom of answer) which is much harder to spoof than an ID. A sequential user ID gives someone an upper hand in understand how to locate other users and possibly get around application security. No I don't have examples handy, just doesn't smell good to me.
If you don't want to roll your own and it is possible to adjust your application I would suggest just exposing another property to your ApplicationModel
class. This property would be responsible for converting the string Id
column from IdentityUser
to a Guid
. In addition to this I would override the Id
property (although keep the logic the same) to add a nice intelisense hint that the ID
property should be used.
public class ApplicationUser : IdentityUser
{
/// <summary>
/// The unique User GUID
/// </summary>
[NotMapped]
public Guid ID
{
get { return Guid.Parse(this.Id); }
set { this.Id = value.ToString(); }
}
/// <summary>
/// weak userid reference. Use the property <seealso cref="WebApplication6.Models.ID"/> instead
/// </summary>
public override string Id { get { return base.Id; } set { base.Id = value; } }
}
As you see above we just add another property ID
of type Guid
with the NotMapped
attribute. (The attribute is to prevent EF from trying to store this value) The getters
and setters
of this property just parse the Guid
of the base string Id
.
Now this is just an idea, you could happily (and probably a better option) write your own as you see fit for your application.
Appendix 1 : IndentityUser
constructor.
Below is the constructor a new IdentityUser
from the Microsoft.AspNet.Identity.EntityFramework.
assembly: note the id is set to Guid.NewGuid().ToString()
public IdentityUser()
{
this.Id = Guid.NewGuid().ToString();
this.Claims = new List<IdentityUserClaim>();
this.Roles = new List<IdentityUserRole>();
this.Logins = new List<IdentityUserLogin>();
}