RoleProvider.IsUserInRole(String, String) 메서드

정의

구성된 applicationName에 대해 지정된 사용자가 지정된 역할에 속하는지 여부를 나타내는 값을 가져옵니다.

public:
 abstract bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public abstract bool IsUserInRole (string username, string roleName);
abstract member IsUserInRole : string * string -> bool
Public MustOverride Function IsUserInRole (username As String, roleName As String) As Boolean

매개 변수

username
String

검색할 사용자 이름입니다.

roleName
String

검색할 역할입니다.

반환

Boolean

지정된 사용자가 구성된true 에 대해 지정된 역할에 속하면 applicationName이고, 속하지 않으면 false입니다.

예제

다음 코드 예제에서는 메서드의 샘플 구현을 IsUserInRole 보여줍니다.

public override bool IsUserInRole(string username, string rolename)
{
  if (username == null || username == "")
    throw new ProviderException("User name cannot be empty or null.");
  if (rolename == null || rolename == "")
    throw new ProviderException("Role name cannot be empty or null.");

  bool userIsInRole = false;

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

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

  try
  {
    conn.Open();

    int numRecs = (int)cmd.ExecuteScalar();

    if (numRecs > 0)
    {
      userIsInRole = true;
    }
  }
  catch (OdbcException)
  {
    // Handle exception.
  }
  finally
  {
    conn.Close();      
  }

  return userIsInRole;
}
Public Overrides Function IsUserInRole(ByVal username As String, ByVal rolename As String) As Boolean
    If username Is Nothing OrElse username = "" Then _
      Throw New ProviderException("User name cannot be empty or null.")
    If rolename Is Nothing OrElse rolename = "" Then _
      Throw New ProviderException("Role name cannot be empty or null.")

    Dim userIsInRole As Boolean = False

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

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

    Try
        conn.Open()

        Dim numRecs As Integer = CType(cmd.ExecuteScalar(), Integer)

        If numRecs > 0 Then
            userIsInRole = True
        End If
    Catch e As OdbcException
        ' Handle exception.
    Finally
        conn.Close()
    End Try

    Return userIsInRole
End Function

설명

IsUserInRole 메서드는 클래스의 Roles 메서드에 의해 IsUserInRole 호출되어 현재 로그온한 사용자가 구성된 ApplicationName데이터 원본의 역할과 연결되어 있는지 여부를 확인합니다.

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

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

적용 대상

추가 정보