共用方式為


RoleProvider.GetRolesForUser(String) 方法

定義

取得所設定 applicationName 之指定使用者所屬的角色清單。

public:
 abstract cli::array <System::String ^> ^ GetRolesForUser(System::String ^ username);
public abstract string[] GetRolesForUser (string username);
abstract member GetRolesForUser : string -> string[]
Public MustOverride Function GetRolesForUser (username As String) As String()

參數

username
String

要傳回角色清單的使用者。

傳回

String[]

字串陣列,包含所設定 applicationName 之指定使用者所屬的所有角色名稱。

範例

下列程式代碼範例示範 方法的範例實作 GetRolesForUser

public override string[] GetRolesForUser(string username)
{
  if (username == null || username == "")
    throw new ProviderException("User name cannot be empty or null.");

  string tmpRoleNames = "";

  OdbcConnection conn = new OdbcConnection(connectionString);
  OdbcCommand cmd = new OdbcCommand("SELECT Rolename FROM UsersInRoles "  +
                                    " WHERE Username = ? AND ApplicationName = ?", conn);

  cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username;
  cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;

  OdbcDataReader reader = null;

  try
  {
    conn.Open();

    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
      tmpRoleNames += reader.GetString(0) + ",";
    }
  }
  catch (OdbcException)
  {
    // Handle exception.
  }
  finally
  {
    if (reader != null) { reader.Close(); }
    conn.Close();      
  }

  if (tmpRoleNames.Length > 0)
  {
    // Remove trailing comma.
    tmpRoleNames = tmpRoleNames.Substring(0, tmpRoleNames.Length - 1);
    return tmpRoleNames.Split(',');
  }

  return new string[0];
}
Public Overrides Function GetRolesForUser(ByVal username As String) As String()
    If username Is Nothing OrElse username = "" Then _
      Throw New ProviderException("User name cannot be empty or null.")

    Dim tmpRoleNames As String = ""

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

    cmd.Parameters.Add("@Username", OdbcType.VarChar, 255).Value = username
    cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName

    Dim reader As OdbcDataReader = Nothing

    Try
        conn.Open()

        reader = cmd.ExecuteReader()

        Do While reader.Read()
            tmpRoleNames &= reader.GetString(0) & ","
        Loop
    Catch e As OdbcException
        ' Handle exception.
    Finally
        If Not reader Is Nothing Then reader.Close()
        conn.Close()
    End Try

    If tmpRoleNames.Length > 0 Then
        ' Remove trailing comma.
        tmpRoleNames = tmpRoleNames.Substring(0, tmpRoleNames.Length - 1)
        Return tmpRoleNames.Split(CChar(","))
    End If

    Return New String() {}
End Function

備註

GetRolesForUserGetRolesForUser 類別的 Roles 方法呼叫,從數據源擷取指定使用者相關聯的角色名稱。 只會擷取所設定 ApplicationName 的角色。

如果所設定 applicationName之指定用戶沒有任何角色存在,建議您的提供者傳回沒有元素的字串陣列。

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

適用於

另請參閱