Restrict certain ToolStripMenu items to certain users

98 views Asked by At

Using VB.NET 2010:

I'm making a simple "launcher" application for our work applications. I have already created the ToolStripMenu that contains the menus I want. They will be arranged like so:

File | Dept 1 | Dept 2 | Dept 3 | Admin | Help

What I need to do, is restrict access to the Admin menu, based on the Environment.UserName variable. Ideally, I would like to have it not even render - but if the only option is to have it greyed out, I am okay with that as well.

1

There are 1 answers

0
Hans Passant On
    public Form1() {
        InitializeComponent();
        var id = System.Security.Principal.WindowsIdentity.GetCurrent();
        var prince = new System.Security.Principal.WindowsPrincipal(id);
        adminToolStripMenuItem.Visible = prince.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
    }

There are a rather large number of ways that a LAN admin can move that cheese. You can tell from the very awkward code I posted. Querying the domain controller with the classes in the System.DirectoryServices is often necessary.