AddUsersToRoles Method

RoleProvider.AddUsersToRoles Method

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

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

public abstract void AddUsersToRoles (
	string[] usernames,
	string[] roleNames
)
public abstract void AddUsersToRoles (
	String[] usernames, 
	String[] roleNames
)
public abstract function AddUsersToRoles (
	usernames : String[], 
	roleNames : String[]
)
Not applicable.

Parameters

usernames

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

roleNames

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

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 roleNames specified are not found for the configured applicationName, we recommend that your provider throw a ProviderException.

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

If any of the specified usernames is a null reference (Nothing in Visual Basic) or is an empty string, we recommend that your provider throw an exception.

If any of the specified roleNames is a null reference (Nothing in Visual Basic) 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.

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();      
  }
}

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2016 Microsoft