RoleProvider.AddUsersToRoles(String[], String[]) 方法

定义

向配置的 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())

参数

usernames
String[]

要添加到指定的角色的用户名的字符串数组。

roleNames
String[]

要添加指定的用户名的角色名称的字符串数组。

示例

下面的代码示例演示 了 方法的示例 AddUsersToRoles 实现。

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

注解

AddUsersToRolesRoles 类调用,以将指定用户与数据源中的指定角色相关联。 角色将添加到配置的 ApplicationName

如果未为配置的 applicationName找到任何指定角色名称,建议提供程序引发 ProviderException

如果任何指定的用户名未与配置 applicationName的任何指定角色名称相关联,建议提供程序引发 ProviderException

如果任何指定的用户名为 null 或为空字符串,建议提供程序引发异常。

如果任何指定角色名称为 null 或为空字符串,建议提供程序引发异常。

如果数据源支持事务,建议在事务中包含每个添加操作,并回滚事务,并在任何添加操作失败时引发异常。

适用于

另请参阅