Redigera

Dela via


RoleProvider.AddUsersToRoles(String[], String[]) Method

Definition

Adds the specified user names to the specified roles for the configured applicationName.

public:
 abstract void AddUsersToRoles(cli::array <System::String ^> ^ usernames, cli::array <System::String ^> ^ roleNames);
public abstract void AddUsersToRoles (string[] usernames, string[] roleNames);
abstract member AddUsersToRoles : string[] * string[] -> unit
Public MustOverride Sub AddUsersToRoles (usernames As String(), roleNames As String())

Parameters

usernames
String[]

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

roleNames
String[]

A string array of the role names to add the specified user names to.

Examples

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

public override void AddUsersToRoles(string[]  usernames, string[] rolenames)
{
  foreach (string rolename in rolenames)
  {
    if (rolename == null || rolename == "")
      throw new ProviderException("Role name cannot be empty or null.");
    if (!RoleExists(rolename))
      throw new ProviderException("Role name not found.");
  }

  foreach (string username in usernames)
  {
    if (username == null || username == "")
      throw new ProviderException("User name cannot be empty or null.");
    if (username.Contains(","))
      throw new ArgumentException("User names cannot contain commas.");

    foreach (string rolename in rolenames)
    {
      if (IsUserInRole(username, rolename))
        throw new ProviderException("User is already in role.");
    }
  }

  OdbcConnection conn = new OdbcConnection(connectionString);
  OdbcCommand cmd = new OdbcCommand("INSERT INTO UsersInRoles "  +
                                    " (Username, Rolename, ApplicationName) " +
                                    " Values(?, ?, ?)", conn);

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

  try
  {
    conn.Open();

    foreach (string username in usernames)
    {
      foreach (string rolename in rolenames)
      {
        userParm.Value = username;
        roleParm.Value = rolename;
        cmd.ExecuteNonQuery();
      }
    }
  }
  catch (OdbcException)
  {
    // Handle exception.
  }
  finally
  {
    conn.Close();      
  }
}
Public Overrides Sub AddUsersToRoles(usernames As String(), 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.")
    If username.Contains(",") Then _
      Throw New ArgumentException("User names cannot contain commas.")

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


  Dim conn As OdbcConnection = New OdbcConnection(connectionString)
            Dim cmd As OdbcCommand = New OdbcCommand("INSERT INTO UsersInRoles " & _
                                           " (Username, Rolename, ApplicationName) " & _
                                           " Values(?, ?, ?)", 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

Remarks

AddUsersToRoles is called by the Roles class to associate the specified users with the specified roles at the data source. Roles are added to the configured ApplicationName.

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 null or is an empty string, we recommend that your provider throw an exception.

If any of the specified role names is null 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 add operation in a transaction and that you roll back the transaction and throw an exception if any add operation fails.

Applies to

See also