2017-11-25 197 views
3

我是新來的MVC,我有我的webserve這個問題,我似乎無法修復。 在我的Dbinit中,我向用戶表添加了一個用戶,並將一些成分添加到了我製作的成分表中。 配料被添加到表中沒有任何問題。 但是,用戶根本不會被添加(dbo.AspNetUsers爲空)。MVC IdentityServer Db空Usertable /錯誤播種

當我運行該程序時,在我的Initialize任務(在DbInitializer中)> InvalidOperationException中出現異常,並顯示錯誤消息「Error Seeding Database」。這是一個例外,我已經實現了我自己的Program.cs:

public static void Main(string[] args) 
    { 
     var host = BuildWebHost(args); 

     using (var scope = host.Services.CreateScope()) 
     { 
     var services = scope.ServiceProvider; 
     try 
     { 
      var context = services.GetRequiredService<AppDbContext>(); 
      var dbInit = services.GetRequiredService<IDBInitializer>(); 

      dbInit.Initialize(context).GetAwaiter().GetResult(); 
     } 
     catch (Exception ex) 
     { 
      var log = services.GetRequiredService<ILogger<Program>>(); 
      log.LogError(ex, "Error seeding Database."); 
     } 
     } 

     host.Run(); 
    } 

我不知道這種異常是否與我的問題,但我一直在試圖解決這個問題。 我的DbIinitializer繼承自一個只包含任務定義的接口。 我在Startup.cs中調用IDBInitializer和DBInitializer,其中DBInitializer失敗。 這裏是我的DBInitializer代碼:

namespace mvcServer.Data 
{ 
    public class DBInitializer : IDBInitializer 
    { 
     private readonly UserManager<User> userManager; 
     private readonly RoleManager<IdentityRole> roleManager; 

     public DBInitializer(UserManager<User> userManager, RoleManager<IdentityRole> roleManager) 
     { 
     this.userManager = userManager; 
     this.roleManager = roleManager; 
     } 

    public async Task Initialize(AppDbContext context) 
     { 
     context.Database.EnsureCreated(); 

     // Look for any users 
     if (context.Users.Any()) 
     { 
      return; // Db has been seeded. 
     } 

     // Create roles 
     await roleManager.CreateAsync(new IdentityRole("user")); 

     // Populate Ingredients table 
     context.Ingredients.AddRange(
     new Ingredient { Name = "Potato", Category = "Vegetable", Allergen = "Gluten", IsSafe = true }, 
     new Ingredient { Name = "Strawberry", Category = "Fruit", Allergen = "Sugar", IsSafe = false }); 

     await context.SaveChangesAsync(); 

     var user = new User 
     { 
      Name = "Andrea", 
      Lastname = "X", 
      AccessFailedCount = 0, 
      Email = "[email protected]", 
      NormalizedEmail = "[email protected]", 
      EmailConfirmed = false, 
      LockoutEnabled = true, 
      TwoFactorEnabled = false, 
      UserName = "andrea", 
      NormalizedUserName = "ANDREA" 
     }; 

     var userResult = await userManager.CreateAsync(user, "Password123"); 
     var roleresult = await userManager.AddToRoleAsync(user, "user"); 

     if (userResult.Succeeded) 
     { 
      var currUser = await userManager.FindByNameAsync(user.UserName); 

      // Assigns claims for security 
      var claims = new List<Claim> { 
         new Claim(type: JwtClaimTypes.GivenName, value: user.Name), 
         new Claim(type: JwtClaimTypes.FamilyName, value: user.Lastname), 
        }; 
      await userManager.AddClaimsAsync(currUser, claims); 

     } 
     else 
     { 
      Debug.WriteLine(userResult.ToString()); 
     } 
    } 
    } 
} 

是不是因爲我用的,而不是異步方法context.Ingredients.AddRange添加的成分?儘管我已經嘗試過,但它什麼也沒做。 我也不確定是否需要丟棄我的遷移,並在更改此類內容時完全重新啓動。

這裏是Exception

System.InvalidOperationException 
Message: Error seeding Database. Failed method: 
Microsoft.AspNetCore.Identity.UserManager'1+<ValidateUserInternal>d_169.MoveNext 

如果我需要提供更多的代碼或其他任何東西讓我知道。

編輯: AppDbContext:

namespace mvcServer.Data 
{ 
    public class AppDbContext : IdentityDbContext<User> 
    { 
     public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) 
     { 
     } 

    public DbSet<Ingredient> Ingredients { get; set; } 
    } 
} 
+0

,什麼是AppDbContext? – derloopkat

+0

我在其中添加了編輯@derloopkat中的代碼 - 它只是設置數據庫「Ingredients」和Identity Server中的標準Identity數據庫 – Axelle

+0

您正在使用Core Identity(它是Microsoft Framework的一部分)進行用戶驗證。像IdentityDbContext和其他類的目的是爲了驗證和存儲信息,如證書,用戶,角色等。我不認爲這是爲了存儲來自你的應用程序的定製數據(例如你試圖附加的成分列表),儘管這是可能的在對模型和類進行更改之後。 – derloopkat

回答

0

經過大量的搜索,我發現答案感謝this崗位。

如該線程回答用戶「達菲朋克」:

我離開這個位置的人可能都有過類似的問題。 我有完全一樣的「症狀」。事實證明,與存儲的密碼相關的問題 不符合配置的 密碼策略(至少一個大寫char,一個小寫char等 等)。

根據以下評論和答案,一般情況下,如果沒有遵循任何用戶創建約束條件或 策略,則會拋出相同的模糊錯誤 消息。

這可能包括以下內容:不遵守

密碼策略(這似乎是最常見的原因)通過爲空字符串傳遞 必填項/空重複 用戶名或電子郵件實在是一個大範圍可能導致此錯誤發生的問題。如果以上操作不解決您的問題,我 建議如下:

熟悉,因爲它是爲您的解決方案按以下@ Ted的答案的規則和政策identityprovider ,跟蹤 或將斷點在UserManager.Create方法上查看 結果,因爲這可能會揭示問題的原因。

在我的情況下,用戶我創建的密碼根本不能滿足我提出的要求:)