2013-03-01 3 views
1

에 대한 Active Directory 그룹 ID를받을 수 있나요 : 문제는 이 어떻게 허가 된 사용자

[Authorize(Roles = "AD_group1, AD_group2")] 

됩니다

- 어떤 방법이 내가 할 수있다 권한이있는 사용자 (int 또는 string과 상관없이)에 대해 Active Directory groupId을 얻으시겠습니까?

upd : 기본적인 아이디어는 데이터베이스에 테이블을 저장하는 것입니다. 템플릿은 각 그룹마다 별도로 있어야합니다. 예 : group1의 사용자는 일반적인 질문에 대한 빠른 대답을위한 템플릿을 가지고있을 수 있지만 group2에는 템플릿이 없거나 다른 템플릿이 있습니다.

답변

3

.NET 3.5 이상인 경우 System.DirectoryServices.AccountManagement을 확인해야합니다 S.DS.AM) 네임 스페이스. 여기에 대한 모든 읽기 :

// set up domain context 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // find a user 
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 
    // or if you want the currently logged in user - you can also use: 
    // UserPrincipal user = UserPrincipal.Current; 

    if(user != null) 
    { 
     // get all groups the user is a member of 
     foreach(GroupPrincipal group in user.GetAuthorizationGroups()) 
     { 
      string distinguishedName = group.DistinguishedName; 
      Guid groupGuid = group.Guid; 
     } 
    } 
} 

을 : MSDN docs on System.DirectoryServices.AccountManagement

기본적으로

+0

감사합니다. 그게 내가 필요한 것입니다. – Sergio

+0

프로덕션에 오류가 있습니다. System.InvalidCastException : 'System.DirectoryServices.AccountManagement.GroupPrincipal'유형의 객체를 'System.DirectoryServices.AccountManagement.UserPrincipal'을 (를) 입력 할 수 없습니다? 무엇을 해야할지 알고 있습니까? –