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

作为搜索范围的角色。

返回

如果指定用户属于已配置的 applicationName 的指定角色,则为 true;否则为 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

注解

方法 IsUserInRoleIsUserInRole 类的 Roles 方法调用,以确定当前登录用户是否与所配置的 ApplicationName数据源中的角色相关联。

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

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

适用于

另请参阅