共用方式為


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

備註

AddUsersToRoles 由類別呼叫, Roles 以將指定的用戶與數據源上的指定角色產生關聯。 角色會新增至已設定 ApplicationName的 。

如果找不到已設定 applicationName的任何指定角色名稱,我們建議您的提供者擲回 ProviderException

如果任何指定的使用者名稱未與所設定 applicationName的任何指定角色名稱相關聯,建議您的提供者擲回 ProviderException

如果任何指定的用戶名稱是 null 或是空字串,建議您的提供者擲回例外狀況。

如果任何指定的角色名稱是 null 或是空字串,建議您提供者擲回例外狀況。

如果您的數據源支援交易,建議您在交易中包含每個新增作業,並復原交易,並在任何新增作業失敗時擲回例外狀況。

適用於

另請參閱