Question

How does one change the PK column of the AspNetUser table from a guid to int data type? This should now be possible with the latest asp.net-identity version which got released today.

But I can't find anywhere how this is done?

Était-ce utile?

La solution

By default the ASP.NET Identity (using Entity Framework) uses strings as the primary keys, not GUIDs, but it does store GUIDs in those string.

You need to define a few more classes, I've just created a new project (I'm using the VS2013 Update 2 CTP), here are the identity models you need to change:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationUserRole : IdentityUserRole<int>
{
}

public class ApplicationUserLogin : IdentityUserLogin<int>
{
}

public class ApplicationUserClaim : IdentityUserClaim<int>
{
}

public class ApplicationRole : IdentityRole<int, ApplicationUserRole>
{
}

public class ApplicatonUserStore :
    UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicatonUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class ApplicationDbContext
    : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

You'll also need to update a few other places, just follow the compile errors, the most common change will be the need to convert the string returned form User.Identity.GetUserId() to an integer.

Also, In answering another question (I did ask the question though), I've provided an example solution that does just this, see the repository below:

https://github.com/JSkimming/AspNet.Identity.EntityFramework.Multitenant

Autres conseils

if anyone finds this while looking for identity 3.0 guide:

public class ApplicationUser : IdentityUser<int>
{
}

public class ApplicationRole : IdentityRole<int>
{
}

public class ApplicationDbContext: 
    IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
}

in Startup.cs file, ConfigureServices method:

services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddEntityFrameworkStores<ApplicationDbContext, int>()//, int being the only difference
    .AddDefaultTokenProviders();

and that's all to it, no need to create your managers like in 2.0

found this in this blog post.

have a look at this answer by hao-kung in this post

So if you want int ids, you need to create your own POCO IUser class and implement your IUserStore for your custom IUser class in the 1.0 RTM release.

This is something we didn't have time to support, but I'm looking into making this easy(ier) in 1.1 right now. Hopefully something will be available in the nightly builds soon.

Updated with 1.1-alpha1 example: How to get nightly builts

If you update to the latest nightly bits, you can try out the new 1.1-alpha1 apis which should make this easier now: Here's what plugging in Guids instead of strings should look like 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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top