문제

명시 적으로 확인하지 않고 Windows 인증 사용자가있는 역할 목록을 얻는 방법이 있습니까? WindowsPrincipal.IsInRole 방법?

도움이 되었습니까?

해결책

WindowsPrincipal.IsInRole 사용자가 해당 이름을 가진 그룹의 구성원인지 확인하십시오. Windows 그룹 a 역할. 사용자가 WindowsIdentity.Groups 재산.

당신은 얻을 수 있습니다 WindowsIdentity 당신의 WindowsPrincipal:

WindowsIdentity identity = WindowsPrincipal.Identity as WindowsIdentity;

또는 WindowsIndity의 공장 방법에서 얻을 수 있습니다.

WindowsIdentity identity = WindowsIdentity.GetCurrent();

WindowsIdenity.Groups 모음입니다 IdentityReference 단지 그룹의 SID를 제공합니다. 그룹 이름이 필요한 경우 번역해야합니다. IdentityReferenceNTAccount 그리고 가치를 얻으십시오 :

var groupNames = from id in identity.Groups
                 select id.Translate(typeof(NTAccount)).Value;

다른 팁

편집 : 조쉬가 나를 이겼다! :)

이 시도

using System;
using System.Security.Principal;

namespace ConsoleApplication5
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var identity = WindowsIdentity.GetCurrent();

            foreach (var groupId in identity.Groups)
            {
                var group = groupId.Translate(typeof (NTAccount));
                Console.WriteLine(group);
            }
        }
    }
}

도메인 서버에 연결되지 않은 경우 Translate 함수는 다음과 같은 예외를 던질 수 있습니다 The trust relationship between this workstation and the primary domain failed.

그러나 대부분의 그룹에서는 괜찮을 것이므로 사용합니다.

foreach(var s in WindowsIdentity.GetCurrent().Groups) {
    try {
        IdentityReference grp = s.Translate(typeof (NTAccount)); 
        groups.Add(grp.Value);
    }
    catch(Exception) {  }
}

ASP.NET MVC 사이트에서는 다음과 같이 할 수 있습니다.

이것을 web.config에 추가하십시오 :

<system.web>
  ...
  <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" />
  ...
</system.web>

그런 다음 사용할 수 있습니다 Roles.GetRolesForUser() 사용자가 구성원 인 모든 Windows 그룹을 얻으려면 당신이 확인하십시오 using System.Web.Security.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top