2016-07-07 5 views
0

Webアプリケーションの起動時に、2つのロールと2人のユーザーを作成するために、以下のメソッドを使用しました(Global.asax.csのApplication_Start())。 ただし、管理者ロールは作成されていますが、ユーザーロールは作成されていません。 [email protected]という名前のユーザーと[email protected]という名前のユーザーにも同様のことが起こります。最初のものは作成されますが、2番目のものは作成されません。ASP.Net IDで役割とユーザーを作成する方法は?

ここに私のコードです。

void create() { 
    ApplicationDbContext context = new ApplicationDbContext(); 
    IdentityResult IdRoleResult; 
    IdentityResult IdUserResult; 

    var roleStore = new RoleStore<IdentityRole>(context); 
    var roleMngr = new RoleManager<IdentityRole>(roleStore); 
    if (!roleMngr.RoleExists("Administrator")) 
     IdRoleResult = roleMngr.Create(new IdentityRole("Administrator")); 

    roleStore = new RoleStore<IdentityRole>(context); 
    roleMngr = new RoleManager<IdentityRole>(roleStore); 
    if (!roleMngr.RoleExists("User")) 
     IdRoleResult = roleMngr.Create(new IdentityRole("User")); 

    var userMngr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); 
    var appUser = new ApplicationUser() { UserName = "[email protected]" }; 
    IdUserResult = userMngr.Create(appUser, "pa$$word"); 
    if (IdUserResult.Succeeded) 
     IdRoleResult = userMngr.AddToRole(appUser.Id, "Administrator"); 

    userMngr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); 
    appUser = new ApplicationUser() { UserName = "[email protected]" }; 
    IdUserResult = userMngr.Create(appUser, "user"); 
    if (IdUserResult.Succeeded) 
     IdRoleResult = userMngr.AddToRole(appUser.Id, "User"); 
} 

私が間違って行ったことやこれを実行する別の方法を教えてもらえますか? ありがとうございます。

更新されたコード:

void createAdmin() { 
    ApplicationDbContext context = new ApplicationDbContext(); 
    IdentityResult IdRoleResult; 
    IdentityResult IdUserResult; 

    var roleStore = new RoleStore<IdentityRole>(context); 
    var roleMngr = new RoleManager<IdentityRole>(roleStore); 

    if (!roleMngr.RoleExists("Administrator")) { 
     IdRoleResult = roleMngr.Create(new IdentityRole("Administrator")); 
     if (!IdRoleResult.Succeeded) 
      throw new Exception("Administrator role wasnt created."); 
    } 

    if (!roleMngr.RoleExists("User")) { 
     IdRoleResult = roleMngr.Create(new IdentityRole("User")); 
     if (!IdRoleResult.Succeeded) 
      throw new Exception("User role wasnt created."); 
    } 

    var userMngr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); 
    ApplicationUser appUser; 

    var q = from user in userMngr.Users 
      where user.UserName == "[email protected]" 
      select user; 
    if (q.Count() == 0) { 
     appUser = new ApplicationUser() { UserName = "[email protected]" }; 
     IdUserResult = userMngr.Create(appUser, "pa$$word"); 
     if (IdUserResult.Succeeded) { 
      IdRoleResult = userMngr.AddToRole(appUser.Id, "Administrator"); 
      if (!IdRoleResult.Succeeded) 
       throw new Exception("Admin user wasn't added to Administrator role."); 
     } else 
      throw new Exception("Admin user wasn't created."); 
    } 

    q = from user in userMngr.Users 
     where user.UserName == "[email protected]" 
     select user; 
    if (q.Count() == 0) { 
     appUser = new ApplicationUser() { UserName = "[email protected]" }; 
     IdUserResult = userMngr.Create(appUser, "user"); 
     if (IdUserResult.Succeeded) { 
      IdRoleResult = userMngr.AddToRole(appUser.Id, "User"); 
      if (!IdRoleResult.Succeeded) 
       throw new Exception("User user wasn't added to User role."); 
     } else 
      throw new Exception("User user wasn't created."); 
    } 
} 

は、ここで私が見つかりましたコードはメッセージと例外をスローして、その「ユーザーユーザーが作成されませんでした。」

+1

'User'ロールが作成されない場合の' IdRoleResult'の値は何ですか?何が起きているのかを確認するには、これらの戻り値を調べる必要があります。 – spender

+0

なぜ各操作のために新しい 'RoleStore'と' RoleManager'インスタンスを作成していますか?これらは再利用できます。 'UserManager'と同じです。 – spender

+1

私は真剣に 'if(IdUserResult.Succeeded)IdRoleResult = userMngr.AddToRole(appUser.Id、" Administrator ");'のようなコードを避けるでしょう。代わりに、 'ArgumentOutOfRangeException'を投げるデフォルトのケースで' switch'ステートメントを実装することを好みます。そうすれば、物事は決して静かに失敗しません。 – mason

答えて

0
throw new Exception("User user wasn't created."); 

私はあなたがオブジェクト結果'IdUserResult'にエラーを読んですべきだと思う、と機能CreateAsync()でユーザーを挿入します。

関連する問題