Mitch Brown
Mitch Brown

Reputation: 25

Restrict certain ToolStripMenu items to certain users

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.

Upvotes: 0

Views: 86

Answers (1)

Hans Passant
Hans Passant

Reputation: 942408

    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.

Upvotes: 1

Related Questions