1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

VB.Net Group Authentication

Discussion in 'Scripting & Programming' started by Fergal1982, May 24, 2007.

  1. Fergal1982

    Fergal1982 Petabyte Poster

    Dont you just love Programming? Its always an opportunity to learn something new.

    For instance, my particular area of 'speciality' is interaction with Active Directory.

    One of my apps performs authentication based on group memberships to allow or deny access to the app. Previously I used a 30-40 line Function which would obtain the username of the person running the app, search AD for their account, bind to their account, and search their group memberships for one of two specific groups.

    of course, in this particular section of code, the iteration is not recursive. Meaning you have to be directly in the group or you wont have access (so if you are in group b, which is a member of group c, you wouldnt get access). I know how I could correct this issue but havent needed to so far.

    Now, the other day I got to thinking about a discussion I'd had with out AD authority, where he informed me that with access to a directory, your account provides a token indicating you are a member of the required group (which works recursively). So i decided to revisit the function and discovered that what I was doing in 30-40 lines, I can achieve in about 5.

    I figured some of you guys might find use of the code in future, so here it is:

    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] WinID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Security.Principal.WindowsIdentity
    WinID = Security.Principal.WindowsIdentity.GetCurrent()
    [/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] User [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Security.Principal.WindowsPrincipal(WinID)[/SIZE]
    Dim Ingroup as Boolean
    [SIZE=2]Ingroup = User.IsInRole("Group A")
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  2. JonnyMX

    JonnyMX Petabyte Poster


    Certifications: MCT, MCTS, i-Net+, CIW CI, Prince2, MSP, MCSD

Share This Page