ASP.NET Identity 2.1 change PK to int error

 

Questions


I have changed the PK as described in http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity, however after I try to login, i get the following error. The login part is successful because “this” contains all the approperate data, however creating identity seems to fail

The specified cast from a materialized ‘System.String’ type to the ‘System.Int32’ type is not valid.

at the line

var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

of the method

 public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> 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 string FirstName { get; set; }
    public string LastName { get; set; }

}

 

 

————————————————-

Answer

I had the same problem as well.. the only difference is that my error is during AddToRole or AddToRoleAsync.

The specified cast from a materialized ‘System.String’ type to the ‘System.Int32’ type is not valid.

The offending line is at: await UserManager.AddToRoleAsync(user.Id, model.RoleId);

As it turns out, I need to change the key for table AspNetRoles as well… perhaps this picture makes it clearer..

enter image description here

asp.net-identity,c#

Facebook Comments

Post a comment