This documentation is archived and is not being maintained.

RoleProvider.RemoveUsersFromRoles Method

Removes the specified user names from the specified roles for the configured applicationName.

Namespace:  System.Web.Security
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public MustOverride Sub RemoveUsersFromRoles ( _
	usernames As String(), _
	roleNames As String() _
)
'Usage
Dim instance As RoleProvider 
Dim usernames As String()
Dim roleNames As String()

instance.RemoveUsersFromRoles(usernames, _
	roleNames)

Parameters

usernames
Type: System.String()

A string array of user names to be removed from the specified roles.

roleNames
Type: System.String()

A string array of role names to remove the specified user names from.

RemoveUsersFromRoles is called by RemoveUserFromRole , RemoveUsersFromRole , RemoveUserFromRoles , and RemoveUsersFromRoles methods of the Roles class to remove the specified users from the specified roles at the data source. Only roles for the configured ApplicationName are modified.

If any of the specified role names are not found for the configured applicationName, we recommend that your provider throw a ProviderException.

If any of the specified user names are not associated with any of the specified role names for the configured applicationName, we recommend that your provider throw a ProviderException.

If any of the specified user names is Nothing or is an empty string, we recommend that your provider throw an exception.

If any of the specified role names is Nothing or is an empty string, we recommend that your provider throw an exception.

If your data source supports transactions, we recommend that you include each remove operation in a transaction and that you roll back the transaction and throw an exception if any remove operation fails.

The following code example shows a sample implementation of the RemoveUsersFromRoles method.

Public Overrides Sub RemoveUsersFromRoles(ByVal usernames As String(), ByVal rolenames As String())

    For Each rolename As String In rolenames
        If rolename Is Nothing OrElse rolename = "" Then _
          Throw New ProviderException("Role name cannot be empty or null.")
        If Not RoleExists(rolename) Then _
          Throw New ProviderException("Role name not found.")
    Next 

    For Each username As String In usernames
        If username Is Nothing OrElse username = "" Then _
          Throw New ProviderException("User name cannot be empty or null.")

        For Each rolename As String In rolenames
            If Not IsUserInRole(username, rolename) Then 
                Throw New ProviderException("User is not in role.")
            End If 
        Next 
    Next 

    Dim conn As OdbcConnection = New OdbcConnection(connectionString)
    Dim cmd As OdbcCommand = New OdbcCommand("DELETE FROM UsersInRoles " & _
                                             " WHERE Username = ? AND Rolename = ? AND ApplicationName = ?", conn)

    Dim userParm As OdbcParameter = cmd.Parameters.Add("@Username", OdbcType.VarChar, 255)
    Dim roleParm As OdbcParameter = cmd.Parameters.Add("@Rolename", OdbcType.VarChar, 255)
    cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName

    Try
        conn.Open()

        For Each username As String In usernames
            For Each rolename As String In rolenames
                userParm.Value = username
                roleParm.Value = rolename
                cmd.ExecuteNonQuery()
            Next 
        Next 
    Catch e As OdbcException
        ' Handle exception. 
    Finally
        conn.Close()
    End Try 
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: