2013-09-30 5 views
5

(ASP의 경우 MVC5 RC) 역할 시스템을 작동시키지 못했습니다. 내 데이터베이스에는 모든 필요 테이블이 있지만 롤에있는 사용자가 항상 false (SQL 예외가 아님)를 반환하는 경우 교정이 필요합니다!ASP의 역할 관리 MVC 5 (Microsoft.AspNet.Identity)

어딘가에 IPrincipal의 역할 시스템을 활성화해야합니까?

테스트 코드 :

AccountController accCont = new AccountController(); 

// check role exist : result = true 
var roleExist = await accCont.IdentityManager.Roles.RoleExistsAsync("61c84919-72e2-4114-9520-83a3e5f09de1"); 

// try find role by name : result = role object 
var role = await accCont.IdentityManager.Roles.FindRoleByNameAsync("ProjectAdministrator"); 

// check with AccountController instance : result = true 
var exist = await accCont.IdentityManager.Roles.IsUserInRoleAsync(User.Identity.GetUserId(), role.Id); 

// check if current user is in role : result (both) = false???? 
var inRole = User.IsInRole(role.Id); 
var inRole2 = User.IsInRole(role.Name); 

는 또한 Microsoft.AspNet.Identity.Owin 네임 스페이스에서 IIdentity.GetUserId() 확장 메서드와 같은 사용자 정의 정상 참작을 구축하려고합니다.

namespace Microsoft.AspNet.Identity 
{ 
    public static class IdentityExtensions 
    { 
     public static string IsUserInRole(this IIdentity identity) 
     { 
      if (identity == null) 
      { 
       throw new ArgumentNullException("identity"); 
      } 
      ClaimsIdentity identity2 = identity as ClaimsIdentity; 
      if (identity2 != null) 
      { 
       var result = identity2.FindFirstValue(IdentityConfig.Settings.GetAuthenticationOptions().RoleClaimType); 

       return null; // later result 
      } 
      return null; 
     } 
    } 
} 

그러나 청구 유형 RoleClaimType에 대한 결과는 항상 null입니다 :(난 정말이 붙어있어 .

당신의 도움을 주셔서 감사합니다! 스테

+0

User.IsInRole ("string role name")을 사용해 보셨나요? – nocturns2

+1

또는 시도해 볼 수 있습니다 : string [] userroles = Roles.GetRolesForUser ("username") - 사용자 롤에 배열을 반환해야 순환하여 특정 역할이 포함되어 있는지 확인할 수 있습니다. – nocturns2

답변

2

User.IsInRole 기본적으로 주장보고있다 현재 로그인 한 사용자의 경우 로그인 정보는 어떻게 보이나요? 이는 사용자 ID로 변환되는 쿠키를 작성하는 책임이 있습니다. 따라서 IsInRole 메서드가 제대로 작동하려면 역할 소유권을 올바르게 설정해야합니다.

+0

나는 이것을 필요로한다!! – janhartmann

+1

@JeremyCook 당신은 해결책을 얻었습니까? 기본 엔터티 프레임 워크 하나를 사용하는 대신 사용자 지정 사용자 저장소를 구현했습니다. [Authorize (Roles = "Admin")]이 실행될 때 역할 관련 메서드가 호출되지 않음 –

+0

내 문제의 출처를 찾고 답변을 게시했습니다 (http://stackoverflow.com/questions/20428405/). why-isnt-authorizeroles-admin-working-in-mvc-5-rtm-asp-net-identit/20433316 # 20433316)를 사용하십시오. –

2

MVC 5에서 역할을 사용하는 방법을 이해하려고합니다. 귀하의 질문에 대답 할 수는 없지만이 링크를 확인하십시오. 다운로드 된 솔루션은 바로 사용 즉시 작동하며 일부 코드를 잘라내어 붙여 넣기가 가능하여 내 응용 프로그램에서 작동시킬 수 있습니다. 이제 나는 그것이 무엇을하고 있는지 완전히 이해하려고 노력하고 있습니다.

http://www.typecastexception.com/post/2013/11/11/Extending-Identity-Accounts-and-Implementing-Role-Based-Authentication-in-ASPNET-MVC-5.aspx

그것은 귀하의 질문에 대답하지 않을 수도 있지만, 적어도 그것은 좋은 출발점 그래서, 번거 로움을 많이하지 않고 설명 된대로 실제로 작업을 수행 완벽하게 작동 솔루션입니다.