2016-04-24 35 views
1

我正在使用Identity 3.我想在啓動時種下管理員用戶。使用int id的用戶在Asp.net核心中失敗MVC6

我使用github上的例子SwiftCourier將字符串id轉換爲「int」id。身份3在某些方面有所不同,這適用於SwiftCourier示例中顯示的ID爲「int」的設置,與Identity 2和「int」ID設置相比較。

public class User : IdentityUser<int>{...} 

我的角色之後:

public class Role : IdentityRole<int> 

和startup.cs在配置服務我進入了這個:

我已經把 「INT」 我的用戶之後完成這個

  services.AddIdentity<User, Role>(options => options.User.AllowedUserNameCharacters = null) 
      .AddEntityFrameworkStores<JobsDbContext, int>() 
      .AddUserStore<UserStore<User, Role, JobsDbContext, int>>() 
      .AddRoleStore<RoleStore<Role, JobsDbContext, int>>() 
      .AddDefaultTokenProviders(); 

然後我用它來播種角色,它與整數ID一起工作:

public static void EnsureRolesCreated(this IApplicationBuilder app) 
    { 
     var context = app.ApplicationServices.GetService<JobsDbContext>(); 
     if (context.AllMigrationsApplied()) 
     { 
      var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>(); 

      var Roles = new List<Role>(); 

      Roles.Add(new Role { Name = "Admin", Description = "Users able to access all areas" }); 
      Roles.Add(new Role { Name = "Technician", Description = "Users able to access just the schedule" }); 

      foreach (var role in Roles) 
      { 
       if (!roleManager.RoleExistsAsync(role.Name.ToUpper()).Result) 
       { 
        roleManager.CreateAsync(role); 
       } 
      } 
     } 
    } 

到目前爲止好...我要種子只有一個管理員用戶下手,這是我失敗...

我已經使用了以下#1 example通過@Guy。

我掙扎UserStore這一行:

await new UserStore<User>(context).CreateAsync(userWithRoles.User); 

它失敗的「用戶」,這是我的ApplicationUser只是改名。

我得到的錯誤是:

The type 'JobsLedger.Models.Identity.User' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore<TUser>'. There is no implicit reference conversion from 'JobsLedger.Models.Identity.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser<string>'. 

這似乎是一個事實,即該ID是「int」我認爲

如何讓UserStore工作與「INT」抱怨在這方面??它被設置爲Startup.cs中的int,如上所示。

如果我必須創建一個自定義的「UserStore」,它與「int」一起工作..您如何在Identity 3中執行此操作?

+0

我舉了一個將用戶添加到角色的例子,它有幫助嗎?如果確實如此,請將答案標記爲正確答案 – jamiedanq

+0

我剛剛完成了這項工作。我不能讓用戶在這裏使用int ID創建,更不用說爲用戶添加角色..它在用戶上的失敗.. – si2030

回答

2

它可能不是最完美的解決方案,但這個工程......

我的目標是種子管理員用戶,也將被添加到管理員角色。我已經管理這個,同時避免使用UserStoreand和整數ID。

using JobsLedger.DAL; 
using JobsLedger.Models.Identity; 
using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Identity; 
using Microsoft.Extensions.DependencyInjection; 
using System; 

namespace JobsLedger.Models.Seeding 
{ 
    public static class SeedAdminUser 
    { 
     public static async void EnsureAdminUserCreated(this IApplicationBuilder app) 
     { 
      var context = app.ApplicationServices.GetService<JobsDbContext>(); 
      if (context.AllMigrationsApplied()) 
      { 
       var userManager = app.ApplicationServices.GetService<UserManager<User>>(); 
       var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>(); 

       var user = new User 
       { 
        UserName = "Admin", 
        NormalizedUserName = "ADMIN", 
        Email = "[email protected]", 
        NormalizedEmail = "[email protected]", 
        EmailConfirmed = true, 
        LockoutEnabled = false, 
        SecurityStamp = Guid.NewGuid().ToString() 
       }; 

       var password = new PasswordHasher<User>(); 
       var hashed = password.HashPassword(user, "password"); 
       user.PasswordHash = hashed; 

       var result = await userManager.CreateAsync(user, user.PasswordHash); 

       var AdminUser = await userManager.FindByNameAsync("Admin"); 

       if (AdminUser != null) 
       { 
        if (roleManager.RoleExistsAsync("Admin").Result) 
        { 
         var RoleResult = await userManager.AddToRoleAsync(AdminUser, "Admin"); 
        } 
       } 
      } 
     } 
    } 
} 

我也放在這個進入StartUp.cs下課 「app.UseIdentity();」:

app.EnsureAdminUserCreated(); 

... Models.Identity具有 「用戶」 類,並這就是爲什麼它包含在「使用」中。

您還需要添加以下,使這項工作:

using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Identity; 
using Microsoft.Extensions.DependencyInjection; 
using System; 

最後,你需要將已經被添加爲每上面的問題..角色