Need to change Asp.Net User User Identifier (RTM) from nvarchar (128) to uniqueidentifier

Using Asp.Net Identity 1.0 (RTM version). The default implementation creates an AspNetUsers table. The column type Id is nvarchar (128).

When the database and tables are created, I just would like to change the type of user ID as uniqueidentifier instead of nvarchar (128). I tried using this .HasColumnType ("uniqueidentifier") in an Override of OnModelCreating ... but it causes errors.

Microsoft points out that this is easy ... but I would not agree ...

http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity

Since you control the database schema, common tasks such as changing table names or changing the data type of primary keys are simple.

Thus, according to their extremely concise and completely non-technical documentation, this seems to be a common task changing the data type of the primary key ... but there seems to be nothing simple about this. Please, help.

+4
c # asp.net-identity asp.net-membership
Nov 11 '13 at 17:14
source share
2 answers

If you upgrade to the last nightly bits, you can try the new 1.1-alpha1 apis, which should make it easier now: here, what Guides include instead of lines should look, for example,

public class GuidRole : IdentityRole<Guid, GuidUserRole> { public GuidRole() { Id = Guid.NewGuid(); } public GuidRole(string name) : this() { Name = name; } } public class GuidUserRole : IdentityUserRole<Guid> { } public class GuidUserClaim : IdentityUserClaim<Guid> { } public class GuidUserLogin : IdentityUserLogin<Guid> { } public class GuidUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { public GuidUser() { Id = Guid.NewGuid(); } public GuidUser(string name) : this() { UserName = name; } } private class GuidUserContext : IdentityDbContext<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { } private class GuidUserStore : UserStore<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { public GuidUserStore(DbContext context) : base(context) { } } private class GuidRoleStore : RoleStore<GuidRole, Guid, GuidUserRole> { public GuidRoleStore(DbContext context) : base(context) { } } [TestMethod] public async Task CustomUserGuidKeyTest() { var manager = new UserManager<GuidUser, Guid>(new GuidUserStore(new GuidUserContext())); GuidUser[] users = { new GuidUser() { UserName = "test" }, new GuidUser() { UserName = "test1" }, new GuidUser() { UserName = "test2" }, new GuidUser() { UserName = "test3" } }; foreach (var user in users) { UnitTestHelper.IsSuccess(await manager.CreateAsync(user)); } foreach (var user in users) { var u = await manager.FindByIdAsync(user.Id); Assert.IsNotNull(u); Assert.AreEqual(u.UserName, user.UserName); } } 
+1
Nov 11 '13 at 20:00
source share

Hao Kung from the ASP.NET team has posted here on https://stackoverflow.com/a/166268/2168 that you need to implement your own IUserStore , but he is working on making it easier for version 1.1.

I believe that you will not only need to implement IUserStore , it seems to me that you need your own implementation of UserStore , since UserStore<TUser> limited to IdentityUser .

If I'm right, that means you need to at least implement IUserStore , IUserPasswordStore , IUserSecurityStampStore and IUserLoginStore .

These are 14 methods, most of which are quite simple to implement. Please note that this will not be a complete replacement for UserStore<TUser> , it is only what you need to support the parts that are used from the AccountController from the default templates.

I have a GitHub project where I tried this myself, i.e. I implemented a UserStore that uses MongoDb instead of EntityFramework to save.

+2
Nov 11 '13 at 19:24
source share



All Articles