User Group

How to retrieve the usergroup of a user
Suppose that i don't know any of the groups defined in the domain (can't use IsInRole ...).

Thanks for your help,
Francesco


Answer this question

User Group

  • rknorp

    This guy used reflection to call a protected method of the Identity class! Totally cool!

    <a href="http://www.breaman.net/archives/000008.html">http://www.breaman.net/archives/000008.html</a>


    using System;
    using System.Security;
    using System.Security.Policy;
    using System.Security.Principal;
    using System.Security.Permissions;
    using System.Reflection;

    namespace GetRoles
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    WindowsIdentity ident = (WindowsIdentity)System.Threading.Thread.CurrentPrincipal.Identity;
    string[] roles = (string[])Util.ExecuteMethod(ident, "GetRoles", null);
    foreach (string role in roles) Console.WriteLine(role);
    }
    }

    class Util
    {
    public static object ExecuteMethod(object objectToInvoke,
    string methodName, object[] paramsForMethod)
    {
    object theReturn = null;
    Type curType = objectToInvoke.GetType();
    MethodInfo methodToRun = curType.GetMethod(methodName,
    BindingFlags.NonPublic |
    BindingFlags.Instance |
    BindingFlags.FlattenHierarchy |
    BindingFlags.Public);

    if (methodToRun != null)
    {
    theReturn = methodToRun.Invoke(objectToInvoke, paramsForMethod);
    }

    return theReturn;
    }

    }
    }


  • User Group