2016-07-07 4 views
0

내 웹 앱이 시작될 때 두 가지 역할과 두 명의 사용자를 만들기 위해 다음 방법을 사용했습니다 (Global.asax.cs의 Application_Start()에서). 그러나 관리자 역할은 생성되지만 사용자 역할은 생성되지 않습니다. [email protected]이라는 이름의 사용자와 [email protected]이라는 사용자에 대해서도 비슷한 결과가 발생합니다. 첫 번째는 생성되지만 두 번째는 생성되지 않습니다.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()와 사용자를 삽입합니다.

관련 문제