How can I retrieve all the roles (groups) a user is a member of?

前端 未结 4 2261
孤独总比滥情好
孤独总比滥情好 2021-01-01 10:42

Is there a way to get a list of roles a Windows authenticated user is in, without explicitly checking by WindowsPrincipal.IsInRole method?

4条回答
  •  情话喂你
    2021-01-01 11:44

    EDIT: Josh beat me to it! :)

    Try this

    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);
                }
            }
        }
    }
    

提交回复
热议问题