|
-
May 12th, 2003, 06:36 PM
#1
Thread Starter
New Member
Determine UserID as member of WinNT usergroup
Someone must have done this before easily.
I need to identify whether a UserID (of Windows 2000) is a member of certain NT usergroup (like SalesManager).
I tried DirectoryEntry and DirectorySearcher in the System.DirectoryServices name spaces, but I cannot get it to work. I also tried WindowsIdentity and WindowsPrincipal.IsInRole but with no correct result. I could not find any sample codes anywhere. Any suggestion????
Helppp ... pleaseeee!.
Appreciate it.
Last edited by qnguyen; May 13th, 2003 at 04:33 PM.
-
May 13th, 2003, 03:16 PM
#2
Fanatic Member
This actually isn't so bad - if you can puzzle out the syntax of WMI, probably easier than using the NetUserGetInfo Windows API. I tried to get NetUserGetInfo to work under .NET and failed, but I had better luck with WMI for things like getting the user account, getting the machine domain and user logged domain, etc.
GO DOWNLOAD THIS:
http://www.microsoft.com/downloads/d...8-6E22115FFAF0
Not only does it make objects browsable in Server Explorer (not all that incredibly useful actually) but also when you drag/drop objects from Server Explorer onto your project it takes a ton of mystery out of how to call the damn things. Also it drops in a large amount of helper code that you will probably not use, and will inflate the size of your executable a great deal, but I found it to be much less painful than trial and error API work (I seriously suck at API programming).
-
Jun 24th, 2003, 10:17 AM
#3
PowerPoster
Try this code. IT works for me.
Code:
Imports System.DirectoryServices
Public Class RoleUser
Private _DisplayName As String = String.Empty
Private _Username As String = String.Empty
Private _Groups() As String
Private _IsAuthenticated As Boolean = False
Private _Domain As String '= "Crmntcms1"
Public Property Domain() As String
Get
Return _Domain
End Get
Set(ByVal Value As String)
_Domain = Value
End Set
End Property
Public ReadOnly Property DisplayName() As String
Get
Return _DisplayName
End Get
End Property
Public ReadOnly Property Username() As String
Get
Return _Username
End Get
End Property
Public ReadOnly Property Groups() As String()
Get
Return _Groups
End Get
End Property
Public ReadOnly Property IsAuthenticated() As Boolean
Get
Return _IsAuthenticated
End Get
End Property
Public Function Authenticate(ByVal username As String, ByVal password As String) As Boolean
Try
If username Is Nothing Then username = String.Empty
If password Is Nothing Then password = String.Empty
Dim de As New DirectoryEntry("LDAP://" & _Domain, username, password, AuthenticationTypes.Secure)
Dim srch As New DirectorySearcher(de)
srch.Filter = "(&(objectClass=user)(samAccountName=" & username & "))"
Dim results As SearchResultCollection = srch.FindAll
Dim res As SearchResult
Dim al As New ArrayList
For Each res In results
Dim obj As Object
For Each obj In res.Properties("MemberOf")
al.Add(TrimToName(obj))
Next
Next
_Groups = al.ToArray(GetType(String))
_Username = res.Properties("samAccountName")(0)
_DisplayName = res.Properties("displayname")(0)
_IsAuthenticated = True
Catch ex As Exception
'MsgBox(ex.Message & ControlChars.NewLine & ex.StackTrace, MsgBoxStyle.Critical, TypeName(ex))
Dim grp() As String
_Groups = grp
_Username = String.Empty
_DisplayName = String.Empty
_IsAuthenticated = False
End Try
Return _IsAuthenticated
End Function
Public Function IsInGroup(ByVal group As String) As Boolean
If _Groups.IndexOf(_Groups, group) > -1 Then Return True
End Function
Public Sub New()
'default
End Sub
Public Sub New(ByVal username As String, ByVal password As String)
Authenticate(username, password)
End Sub
Public Sub New(ByVal domain As String)
_Domain = domain
End Sub
Private Function TrimToName(ByVal path As String) As String
Dim parts() As String = path.Split(",")
Return parts(0).Replace("CN=", String.Empty)
End Function
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|