【问题标题】:Owin identity framework returns no claims when tables are renamed重命名表时,Owin 身份框架不返回任何声明
【发布时间】:2018-03-13 23:24:57
【问题描述】:

如果我在 Visual Studio (asp.net 4.6.1) 中创建一个全新的 MVC 网站,我可以通过在 AccountController 类的 Register() 方法中执行以下操作来添加/检索声明:

if (ModelState.IsValid)
{
    var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
    var result = await UserManager.CreateAsync(user, model.Password);

    var addClaimResult = UserManager.AddClaim(user.Id, new Claim("test", "test"));
    var claims= UserManager.GetClaims(user.Id); 

    //claims.Count == 1
}

如果我通过如下方式覆盖 OnModelCreating 来重命名表,则上面的 claim.Count 为 0。换句话说,一旦我重命名表,我就无法再从数据库中获取声明。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    string prefix = "Identity";
    modelBuilder.Entity<ApplicationUser>().ToTable($"{prefix}Users", "dbo");
    modelBuilder.Entity<IdentityUser>().ToTable($"{prefix}Users", "dbo");
    modelBuilder.Entity<IdentityUserRole>().ToTable($"{prefix}UserRoles", "dbo");
    modelBuilder.Entity<IdentityUserLogin>().ToTable($"{prefix}UserLogins", "dbo");
    modelBuilder.Entity<IdentityUserClaim>().ToTable($"{prefix}UserClaims", "dbo");
    modelBuilder.Entity<IdentityRole>().ToTable($"{prefix}Roles", "dbo");
}

AddClaim 方法成功返回,我可以确认 'test' 已添加到数据库中的表中。与身份框架相关的所有其他内容(登录等)仍然有效,只是添加到数据库中的声明永远不会返回。我已经尝试过 MVC 和 Web API 2,我在不同的时间尝试过(即登录后、授权后、多个 Web 方法调用后等等)。

我启用了实体框架日志记录。 AddClaim 命中数据库。 GetClaims 在任何一种情况下都不会访问数据库(表重命名与否)。

为什么claims.Count = 0?

【问题讨论】:

    标签: asp.net-mvc entity-framework-6 asp.net-web-api2 asp.net-identity claims-based-identity


    【解决方案1】:

    ASP Identity 2 + Web API Token Auth - Persistent claims not Loading

    与上述相同的答案。

    modelBuilder.Entity().ToTable($"{prefix}Users", "dbo");不需要。

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 2013-04-02
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 2017-08-03
      • 1970-01-01
      相关资源
      最近更新 更多