请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
RoleProvider 类
 GetRolesForUser 方法

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
RoleProvider.GetRolesForUser 方法

注意:此方法在 .NET Framework 2.0 版中是新增的。

获取指定用户对于已配置的 applicationName 所属于的角色的列表。

命名空间:System.Web.Security
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public MustOverride Function GetRolesForUser ( _
    username As String _
) As String()
Visual Basic(用法)
Dim instance As RoleProvider
Dim username As String
Dim returnValue As String()

returnValue = instance.GetRolesForUser(username)
C#
public abstract string[] GetRolesForUser (
    string username
)
C++
public:
virtual array<String^>^ GetRolesForUser (
    String^ username
) abstract
J#
public abstract String[] GetRolesForUser (
    String username
)
JScript
public abstract function GetRolesForUser (
    username : String
) : String[]

参数

username

要为其返回角色列表的用户。

返回值

一个字符串数组,其中包含指定用户对于已配置的 applicationName 所属于的所有角色的名称。

GetRolesForUserRoles 类的 GetRolesForUser 方法调用,以从数据源检索指定用户相关联的角色名。仅检索已配置的 ApplicationName 的角色。

如果对于已配置的 applicationName 的指定用户不存在任何角色,则建议提供程序返回不包含任何元素的字符串数组。

如果指定的用户名为 空引用(在 Visual Basic 中为 Nothing) 或是空字符串,建议提供程序引发异常。

下面的代码示例演示 GetRolesForUser 方法的示例实现。

Visual Basic
Public Overrides Function GetRolesForUser(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 [" & usersInRolesTable & "]" & _
                                           " 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
C#
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 [" + usersInRolesTable + "]"  +
                                    " 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];
}

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker