While this is easy to do for the currently logged in user, there does not appear to be a way to do so without iterating through an array of roles or users or writing your own sproc to directly acces the various role and user related tables. Here's code that I recently used (DNN 3.1) to populate one side of a dual listbox with the usernames of those in a comma delimited string of rolenames:
Imports AspNetRoles = Microsoft.ScalableHosting.Security.Roles 'Set AspNetRoles alias
' . . . .
Private Sub PopulateAvailableAuthorizedContacts()
'Note if any users already assigned are to be properly removed from the Available
'AuthorizedContacts listbox, the PopulateAssignedAuthorizedContacts must be called first
Dim UC As New UserController
Dim UsersInRole() As String
Dim Username As String
Dim UserID As String
If Not Settings("editroles") Is Nothing Then
AvailableAuthorizedContacts.Clear()
For Each EditRole As String In CType(Settings("editroles"), String).Split(";"c)
If EditRole.Length > 0 Then
Try
For Each Username In AspNetRoles.GetUsersInRole(EditRole)
Dim User As UserInfo = UC.GetUserByUsername(PortalId, Username)
UserID = User.UserID.ToString
If FindAuthorizedContact(AssignedAuthorizedContacts, UserID) Is Nothing Then
AvailableAuthorizedContacts.Add(New ListItem(Username & " (" & User.FullName & ")", UserID))
End If
Next
Catch ex As Exception
'Eat any exception - can't populate so will return empty or partial list
End Try
End If
Next
With dlcAuthorizedContacts
.DataTextField = "Text"
.DataValueField = "Value"
.Available = AvailableAuthorizedContacts
End With
End If
End Sub
Private Function FindAuthorizedContact(ByVal AuthorizedContacts As ArrayList, ByVal UserID As String) As ListItem
For Each li As ListItem In AuthorizedContacts
If li.Value = UserID Then Return li
Next
Return Nothing
End Function
Note that this code is for DNN 3.1. There was a major change in the role provider between DNN 3.1 (which referenced Microsoft.ScalableHosting.Security.Roles) and DNN 4.0 (which references the built-in ASP.NET 2.0 roles provider (System.Web.Security). I discovered this breaking change when patching FotoVisionDNN's webservice authentication code to work with DNN 4. Here's a section of that code which interates through a module's permissions to see if a specific user is in an authorized role:
Imports AspNetSecurity = System.Web.Security
' . . . .
Dim mpc As New ModulePermissionController
Dim m As ModulePermissionCollection=mpc.GetModulePermissionsCollectionByModuleID(ModuleID)
For Each mp As ModulePermissionInfo In m
If mp.PermissionKey = "EDIT" AndAlso AspNetSecurity.Roles.IsUserInRole(Me.Credentials.Username, mp.RoleName) Then
blnHasAccess = True
'is in acceptable role so set up folder dir . . . etc.
End If
Next
Hope this helps get you started - would sure be nice to have a method in the roles or users controller to do this more easily.