2016-04-24 30 views
1

私はID 3を使用しています。起動時に管理ユーザーをシードしたいと思います。Asp.netコアのMVC6コアIDでユーザーをシードするのに失敗しました

githubのSwiftCourierを例として使用して、文字列IDを「int」IDに変換しました。アイデンティティ3はいくつかの側面で異なり、Identity 2と比較してSwiftCourierの例に示されているように、これはidの "int"の設定に適用され、 "int" IDの設定に適用されます。

私は私のユーザーの後に "INT" を置くことによって、これをやった:私の役割後

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

を:

public class Role : IdentityRole<int> 

と私は、この入力されたコンフィギュレーションサービスでstartup.cs中:

次に、これをシードロールに使用し、整数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つの管理ユーザーをシードしたいと私は失敗するところ、これはある...

私は@Guyにより、以下のStackOverflowのexampleを使用していました。

私はこのラインで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は」私は「int」でUSERSTORE作品を作るにはどうすればよい

思いであるという事実に文句をしているようだ

この点について??上記のようにStartup.csのintに設定されています..

"int"で動作するカスタム "UserStore"を作成する必要がある場合、ID3でどのように行いますか?

+0

私はロールにユーザーを追加する例を挙げましたが、それは役に立ちましたか?それがしたら、plsは正しい答えとして答えをマークします – jamiedanq

+0

私はちょうどそれをしました。私はここで作成されるユーザーをint idを使用して取得するだけで、ユーザーにロールを追加することはできません。 – si2030

答えて

2

これは、最もエレガントな解決策ではないかもしれませんが、これは動作します...

私の目的は、管理者ユーザーをシードしたともadminロールに追加します。私は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; 

最後に、あなたはすでに上記の質問ごととして追加する役割が必要になります。..

関連する問題