Enumerating WindowsIdentity Roles with Reflection
Windows Identity has some properties, but you must use reflection to enumerate the private GetRoles() method:
namespace CurrentUser
{
    using System;
    using System.ComponentModel;
    using System.Reflection;
    using System.Security.Principal;
    using System.Windows.Forms;
 
    public class Form1 : Form
    {
        private TextBox textBox1;
        private Container components = null;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            this.textBox1.Location = new System.Drawing.Point(8, 16);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(320, 384);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "textBox1";
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(344, 414);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
        }
 
        #endregion
 
        [STAThread]
        private static void Main()
        {
            Application.Run(new Form1());
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            WindowsIdentity id = WindowsIdentity.GetCurrent();
            Type idType;
            idType = id.GetType();
            object result =
                idType.InvokeMember("_GetRoles", BindingFlags.Static | BindingFlags.InvokeMethod |
                    BindingFlags.NonPublic, null, id, new Object[] {id.Token}, null);
            string[] roles = (string[]) result;
            int i;
            for (i = 0; i < roles.Length; i++)
                textBox1.Text += roles[i] + "\r\n";
        }
    }
}
-- You can paste the above directly into a Windows Form class to test it out.
 
 
Great job, I got what i'm looking for in your article.
ReplyDeleteThanks so much!