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

설명

GetRolesForUser는 지정된 사용자가 데이터 원본에서 연결된 역할 이름을 검색하기 위해 클래스의 Roles 메서드에 의해 GetRolesForUser 호출됩니다. 구성된 ApplicationName 역할만 검색됩니다.

구성된 applicationName사용자에 대해 지정된 사용자에 대한 역할이 없는 경우 공급자가 요소 없이 문자열 배열을 반환하는 것이 좋습니다.

지정된 사용자 이름이 null 빈 문자열이거나 빈 문자열인 경우 공급자가 예외를 throw하는 것이 좋습니다.

적용 대상

추가 정보