ASP.NET Identity 2.1 change PK to int error

I changed PK as described at http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity , however after trying to login, I got the following error. The login part succeeds because "this" contains all the relevant data, however the identity creation seems to fail.

The specified listing from the materialized type "System.String" of type "System.Int32" is not valid.

in line

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

      

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; }

}

      

+3


source to share


3 answers


I had the same problem. The only difference is that my mistake during AddToRole

or AddToRoleAsync

.

The specified transfer from materialized type "System.String" to type "System.Int32" is not valid.

Insult line: await UserManager.AddToRoleAsync(user.Id, model.RoleId);



As it turns out, I need to change the key for the table as well AspNetRoles

... maybe this picture makes it clearer.

enter image description here

+4


source


Clear your cookies. Your application is likely running before you changed the PK and are now trying to log in using a different "key type" from the cookie.



+3


source


According to your source, I am assuming that you are using ApplicationUserManager to manage all configurations. Use ApplicationUserManager instead of UserManager as shown below. The reason for the GenerateUserIdentityAsync method is being used internally by owin startUp.

Use ApplicationUserManager instead of UserManager.

        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;
        }

      

Hope it helps.

0


source







All Articles