繁体   English   中英

ASP.NET Identity 2.1将PK更改为int错误

[英]ASP.NET Identity 2.1 change PK to int error

我按照http://www.asp.net/identity/overview/extensibility/change-primary-key-for-users-in-aspnet-identity中的说明更改了PK,但是在我尝试登录后,我得到了跟随错误。 登录部分成功,因为“this”包含所有相关数据,但创建身份似乎失败

从物化的“System.String”类型到“System.Int32”类型的指定强制转换无效。

在线

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

}

我也有同样的问题..唯一的区别是我的错误是在AddToRoleAddToRoleAsync期间。

从物化的“System.String”类型到“System.Int32”类型的指定强制转换无效。

违规行位于: await UserManager.AddToRoleAsync(user.Id, model.RoleId);

事实证明,我需要更改表AspNetRoles的密钥......也许这张图片更清晰..

在此输入图像描述

清除你的cookies。 您可能在更改PK之前运行了应用程序,现在尝试使用cookie中的其他“密钥类型”登录。

根据您的来源,我假设您正在使用ApplicationUserManager来管理所有配置。 使用ApplicationUserManager而不是UserManager,如下所示。 在owin startUp中使用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;
        }

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM