【问题标题】:ASP.NET Identity 2.1 change PK to int errorASP.NET Identity 2.1 将 PK 更改为 int 错误
【发布时间】:2015-01-04 18:54:03
【问题描述】:

我已按照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; }

}

【问题讨论】:

  • 你有 ApplicationUserManager 类,它包含 Startup.cs 上的所有配置和 CreatePerOwinContext 吗?

标签: c# asp.net-identity


【解决方案1】:

我也遇到了同样的问题。唯一的区别是我的错误发生在AddToRoleAddToRoleAsync 期间。

从具体化的“System.String”类型到“System.Int32”类型的指定转换无效。

违规线路在:await UserManager.AddToRoleAsync(user.Id, model.RoleId);

事实证明,我还需要更改表 AspNetRoles 的键......也许这张图片更清楚......

【讨论】:

  • 希望您可以为答案添加更多内容
【解决方案2】:

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

【讨论】:

  • 这是我的问题,由于存储了旧的 id 值,必须先清除 cookie 才能工作。另一种解决方案是更改 cookie 名称,这样您就无需依赖所有用户在看到持续的 500 错误时清除其 cookie。
【解决方案3】:

根据您的消息来源,我假设您正在使用 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;
        }

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多