See if user is part of Active Directory group in C# + Asp.net

前端 未结 14 1255
花落未央
花落未央 2020-11-30 19:06

I need a way to see if a user is part of an active directory group from my .Net 3.5 asp.net c# application.

I am using the standard ldap authentication example off o

14条回答
  •  悲&欢浪女
    2020-11-30 19:38

    Here is my 2 cents.

        static void CheckUserGroup(string userName, string userGroup)
        {
            var wi = new WindowsIdentity(userName);
            var wp = new WindowsPrincipal(wi);
    
            bool inRole = wp.IsInRole(userGroup);
    
            Console.WriteLine("User {0} {1} member of {2} AD group", userName, inRole ? "is" : "is not", userGroup);
        }
    

提交回复
热议问题