2016-06-03 6 views
0

Asp.Net5/EntityFramework7에서 Asp.Net Identity를 사용하여 보안을 제공하기 위해 WebAPI 서비스를 만들려고합니다. Asp.Net 5 템플릿을 사용하여 웹 응용 프로그램 프로젝트를 만들었습니다. 그것은 사용자 관리를위한 템플릿 코드를 제공합니다. 이제 나는 역할이나 주장에 대해서도 똑같이하려고 노력하고있다.Asp.net5/EF7의 Asp.Net ID 테이블에 동적으로 역할/클레임을 추가하려면 어떻게합니까?

IdentityRole을 상속 한 ApplicationRole 클래스를 만들었습니다.

public class ApplicationRole: IdentityRole 
    { 
     public ApplicationRole() : base() { } 
     public ApplicationRole(string name, string description) : base(name) 
     { 
      this.Description = description; 
     } 
     public virtual string Description { get; set; } 
    } 

새 역할을 추가하는 내 웹 API 컨트롤러 클래스 메소드

은 다음과 같다 :

[HttpPost("CreateRole", Name = "CreateRole")] 
     public async Task CreateRole([FromBody]string role) 
     { 
      var applicationRole = new ApplicationRole(role, role); 
      var idResult = await _roleManager.CreateAsync(applicationRole); 
      if (idResult.Succeeded) 
      { 
       _logger.LogInformation(3, "Role Created successfully"); 
      } 
      else 
      { 
       var resp = new HttpResponseMessage() 
       { 
        Content = new StringContent("Internal error occurred"), 
        ReasonPhrase = "Internal error occurred", 
        StatusCode = HttpStatusCode.BadRequest 
       }; 
       throw new HttpResponseException(resp); 
      } 
     } 

이 코드 예외

"A database operation failed while processing the request. 
     ArgumentNullException: Value cannot be null. 
     Parameter name: entityType 
     There are pending model changes for ApplicationDbContext 
     Scaffold a new migration for these changes and apply them to the database from the command line: 

     > dnx ef migrations add [migration name] 
     > dnx ef database update 
    " 

와 appication 충돌을 실행하려고 줄에서 충돌합니다. var idResult = await _roleManager.CreateAsync (applicationRole); applicationRole 객체 변수의 빠른보기 값은 attached snapshot에서 확인할 수 있습니다. 위의 라인이 실행되면, 컨트롤은 ApplicationDbContext 클래스로 가서 BuildModel() 메소드를 실행한다. AspNetRoles 테이블을 변경하지는 않았지만 응용 프로그램이 마이그레이션 스크립트를 실행하도록 지시하고 있습니다.이 문제를 해결하는 데 도움을 줄 수 있습니까? Asp.net5/EntityFramework7의 Asp.Net ID 테이블에 역할/클레임을 동적으로 추가하려면 어떻게합니까?

참고 : 나는이 문제에 대한 해결책을 찾을 attached snapshot

+0

의 솔루션을 가지고 : services.AddIdentity () .AddEntityFrameworkStores () .AddDefaultTokenProviders(); – Krishnan

답변

1

발견하십시오 : 런타임 동안으로 roleManager 객체에서 나는 "entityType. \ 연구 \ 이름 nParameter 값은 null 일 수 없습니다"와 같은 오류 메시지가 표시됩니다. ApplicationDbContext 클래스에도 ApplicationRole을 추가해야합니다. 이렇게하면 문제가 해결됩니다.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string> 
    { 
... 
... 
} 

내가 ConfigureServices 방법에서, 나는 다음과 같은 사용자 및 역할 신원을 등록, 포스트 Startup.cs 파일에서 How do I extend IdentityRole using Web API 2 + AspNet Identity 2

관련 문제