Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2006, 10:19 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default AD Credentials

How do you provide Active Directory Credentials to an application? Thanks in advance for any help.
 
Old December 4th, 2006, 10:32 AM
Authorized User
 
Join Date: Nov 2006
Location: , , USA.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Here is an example of obtaining Group Membership for a user.

This code was obtained via web search when I needed the functionality for an application. Please excuse the lack of comments on how the code works. Maybe other forum members with more experience in this area can elaborate on its flow process.

Example: dim ret As Boolean = IsMemberOfGroup("James","Accounting")

    Public Function IsMemberOfGroup(ByVal strUserID As String, ByVal strGroupName As String) As Boolean

        Dim blnIsMemberOfGroup As Boolean = False

        Try

            Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
            Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value

            Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)
            Dim ADSearch As New System.DirectoryServices.DirectorySearcher(ADEntry )

            If strUserID = "" Then
                strUserID = System.Security.Principal.WindowsIdentity.GetCurre nt.Name.Split("\"c)(1)
            End If

            Dim ADSearchResult As System.DirectoryServices.SearchResult = Nothing
            ADSearch.PropertiesToLoad.Add("memberOf")
            ADSearch.Filter = ("(samAccountName=" & strUserID & ")")
            ADSearch.SearchScope = System.DirectoryServices.SearchScope.Subtree
            Dim UserFound As System.DirectoryServices.SearchResult = ADSearch.FindOne()
            Dim propertyCount As Integer = UserFound.Properties("memberOf").Count

            If Not IsNothing(UserFound) Then

                Dim propertyCounter As Integer
                Dim dn As String

                For propertyCounter = 0 To propertyCount - 1

                    dn = CType(UserFound.Properties("memberOf")(propertyCou nter), String)
                    If InStr(dn.ToString, strGroupName) <> 0 Then
                        blnIsMemberOfGroup = True
                    End If
                Next

            End If

        Catch
            '* No error processing here. If an error occurs the function will just return FALSE.
        End Try
        IsMemberOfGroup = blnIsMemberOfGroup
    End Function





Best Regards,
Earl Francis
 
Old December 6th, 2006, 10:49 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Earl,

Thanks for the code...I have had a lot of fun playing with it.
 
Old January 9th, 2007, 08:36 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

I'm new to VB.NET and even more new to AD. But I have been trying and was able to authenticate to AD and now I'm trying to get this group stuff together.

The function looks good, but just how do I use it? I don't think I really understand the "Dim ret As Boolean = IsMemberOfGroup("James", "Accounting")" part. Can someone provide an example please?

Thanks.

====
I what to be a PROGRAMMER!
 
Old January 10th, 2007, 08:32 AM
Authorized User
 
Join Date: Nov 2006
Location: , , USA.
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

dim ret As Boolean = IsMemberOfGroup("James","Accounting")

Regarding the above example:

Let's say that you have written an application for your company's accounting department and you only want users that are a part of that department to access your program. First, you might present a login screen where you will authenticate the user name and password. Once "James" has successfully logged in you next want to make sure that he belongs to a group called "Accounting" which your network administrator has previously set up and has also added users to that group. You would then make a call to this function as follows:

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        '* You have already presented a login screen at this
         point and "James" has successfully logged in.

        If Not IsMemberOfGroup("James","Accounting") then
            Msgbox("You must be a member of the group 'Accounting' in order to access this program!")

            '* Quit the application
            End

        End If

    End Sub

Hopefully this was the information you were looking for.



Best Regards,
Earl Francis




Similar Threads
Thread Thread Starter Forum Replies Last Post
Add existing AD user to AD group via ADSI? thiazi Classic ASP Basics 0 August 24th, 2007 09:27 PM
Regarding credentials Jayapradha .NET Framework 1.x 0 November 7th, 2006 06:42 AM
Render and credentials TonyStoker Reporting Services 0 May 17th, 2006 05:53 PM
Login credentials? mikedeepak Classic ASP Professional 1 February 13th, 2006 08:28 AM
Determine Credentials bmumph C# 2 November 1st, 2005 12:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.