问题
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; }
}
回答1:
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..
回答2:
Clear your cookies. You probably have your application running before you changed PK and now is trying to log in using other 'key type' from the cookie.
回答3:
According to your source I assume that you are using ApplicationUserManager for managing all configurations. Use ApplicationUserManager instead of UserManager as follow. The reason GenerateUserIdentityAsync method used inside the 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 this helps.
来源:https://stackoverflow.com/questions/27769133/asp-net-identity-2-1-change-pk-to-int-error