C# ASP.NET Identity 2.1将PK更改为int错误
我已按中所述更改了PK,但是在尝试登录后,我出现以下错误。登录部分是成功的,因为“this”包含所有批准的数据,但是创建标识似乎失败了 从物化的“System.String”类型到“System.Int32”类型的指定强制转换无效。 排队C# ASP.NET Identity 2.1将PK更改为int错误,c#,asp.net-identity,C#,Asp.net Identity,我已按中所述更改了PK,但是在尝试登录后,我出现以下错误。登录部分是成功的,因为“this”包含所有批准的数据,但是创建标识似乎失败了 从物化的“System.String”类型到“System.Int32”类型的指定强制转换无效。 排队 var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 方法的选择 public class App
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
方法的选择
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; }
}
公共类应用程序用户:IdentityUser
{
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
}
根据您的来源,我假设您正在使用ApplicationUserManager管理所有配置。使用ApplicationUserManager而不是UserManager,如下所示。owin启动中使用的reason GenerateUserIdentityAsync方法
使用ApplicationUserManager而不是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;
}
公共异步任务GenerateUserIdentityAsync(ApplicationUserManager)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
希望这有帮助。清除您的cookies。您可能在更改PK之前运行了应用程序,现在正试图使用cookie中的其他“密钥类型”登录 我也有同样的问题。。唯一的区别是我的错误出现在
AddToRole
或AddToRoleAsync
期间
从具体化的“System.String”类型到“System.Int32”类型的指定强制转换无效
有问题的行位于:await UserManager.AddToRoleAsync(user.Id,model.RoleId)代码>
事实证明,我还需要更改tableAspNetRoles
的键。。。也许这幅图能让事情更清楚
您是否有ApplicationUserManager类,该类包含启动时的所有配置和CreatePerOwinContext.cs?这是我的问题,因为存储了旧的id值,必须先清除Cookie才能正常工作。另一个解决方案是更改cookie名称,这样当用户看到持续的500个错误时,您就不需要依赖所有用户清除cookie。希望您能在答案中添加更多内容