【问题标题】:seed user and user role issue种子用户和用户角色问题
【发布时间】:2016-10-03 22:13:44
【问题描述】:

我正在尝试使用初始用户和用户角色为我的数据库播种,但在这样做时遇到了问题。在查看了关于 SO 的类似帖子后,我在种子方法中得到了以下结果:

if (!context.Users.Any(u => u.UserName == "founder"))
    {
        var store = new UserStore<ApplicationUser>(context);
        var manager = new UserManager<ApplicationUser>(store);
        var user = new ApplicationUser {UserName = "founder"};

        manager.Create(user, "ChangeItAsap!");
        manager.AddToRole(user.Id, "AppAdmin");
    }

还有我的 ApplicationUser 类:

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

我的问题是我在 ApplicationUser 上遇到编译错误,这样说

RecipeManager.Models.ApplicationUser 类型不能用作泛型类型或方法'UserManager&lt;TUser&gt;' 中的类型参数“TUser”。没有从RecipeManager.Models.ApplicationUserMicrosoft.AspNet.Identity.IUser&lt;string&gt; 的隐式引用转换

【问题讨论】:

    标签: c# asp.net-mvc seeding


    【解决方案1】:

    ApplicationUser 必须实现IUser&lt;string&gt; 接口。这是用户管理器中允许的类型。该问题很可能与密钥的string 数据类型有关。默认的脚手架过程假定 string 因为它是更通用的数据类型。您需要将ApplicationUser 类中的键类型更改为字符串,或者继承IUser&lt;int&gt; 或您在UserManager 中使用的任何类型。

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 2015-06-14
      • 2018-08-10
      • 2018-02-28
      • 1970-01-01
      • 2011-11-07
      • 2020-02-25
      • 1970-01-01
      • 2012-11-13
      相关资源
      最近更新 更多