RoleProvider.RemoveUsersFromRoles(String[], String[]) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从适用于配置的 applicationName
的指定的角色移除指定的用户名。
public:
abstract void RemoveUsersFromRoles(cli::array <System::String ^> ^ usernames, cli::array <System::String ^> ^ roleNames);
public abstract void RemoveUsersFromRoles (string[] usernames, string[] roleNames);
abstract member RemoveUsersFromRoles : string[] * string[] -> unit
Public MustOverride Sub RemoveUsersFromRoles (usernames As String(), roleNames As String())
参数
- usernames
- String[]
一个字符串数组,其中包含要从指定的角色移除的用户名。
- roleNames
- String[]
一个字符串数组,其中包含要将指定的用户名从中移除的角色的名称。
示例
下面的代码示例演示 方法的示例 RemoveUsersFromRoles 实现。
public override void RemoveUsersFromRoles(string[] usernames, string[] rolenames)
{
foreach (string rolename in rolenames)
{
if (rolename == null || rolename == "")
throw new ProviderException("Role name cannot be empty or null.");
if (!RoleExists(rolename))
throw new ProviderException("Role name not found.");
}
foreach (string username in usernames)
{
if (username == null || username == "")
throw new ProviderException("User name cannot be empty or null.");
foreach (string rolename in rolenames)
{
if (!IsUserInRole(username, rolename))
throw new ProviderException("User is not in role.");
}
}
OdbcConnection conn = new OdbcConnection(connectionString);
OdbcCommand cmd = new OdbcCommand("DELETE FROM UsersInRoles " +
" WHERE Username = ? AND Rolename = ? AND ApplicationName = ?", conn);
OdbcParameter userParm = cmd.Parameters.Add("@Username", OdbcType.VarChar, 255);
OdbcParameter roleParm = cmd.Parameters.Add("@Rolename", OdbcType.VarChar, 255);
cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName;
try
{
conn.Open();
foreach (string username in usernames)
{
foreach (string rolename in rolenames)
{
userParm.Value = username;
roleParm.Value = rolename;
cmd.ExecuteNonQuery();
}
}
}
catch (OdbcException)
{
// Handle exception.
}
finally
{
conn.Close();
}
}
Public Overrides Sub RemoveUsersFromRoles(ByVal usernames As String(), ByVal rolenames As String())
For Each rolename As String In rolenames
If rolename Is Nothing OrElse rolename = "" Then _
Throw New ProviderException("Role name cannot be empty or null.")
If Not RoleExists(rolename) Then _
Throw New ProviderException("Role name not found.")
Next
For Each username As String In usernames
If username Is Nothing OrElse username = "" Then _
Throw New ProviderException("User name cannot be empty or null.")
For Each rolename As String In rolenames
If Not IsUserInRole(username, rolename) Then
Throw New ProviderException("User is not in role.")
End If
Next
Next
Dim conn As OdbcConnection = New OdbcConnection(connectionString)
Dim cmd As OdbcCommand = New OdbcCommand("DELETE FROM UsersInRoles " & _
" WHERE Username = ? AND Rolename = ? AND ApplicationName = ?", conn)
Dim userParm As OdbcParameter = cmd.Parameters.Add("@Username", OdbcType.VarChar, 255)
Dim roleParm As OdbcParameter = cmd.Parameters.Add("@Rolename", OdbcType.VarChar, 255)
cmd.Parameters.Add("@ApplicationName", OdbcType.VarChar, 255).Value = ApplicationName
Try
conn.Open()
For Each username As String In usernames
For Each rolename As String In rolenames
userParm.Value = username
roleParm.Value = rolename
cmd.ExecuteNonQuery()
Next
Next
Catch e As OdbcException
' Handle exception.
Finally
conn.Close()
End Try
End Sub
注解
RemoveUsersFromRoles由 RemoveUserFromRole 类的 Roles 、 RemoveUsersFromRole 、 RemoveUserFromRoles 和 RemoveUsersFromRoles 方法调用,以从数据源的指定角色中删除指定用户。 仅修改已配置 ApplicationName 的角色。
如果未为配置的 applicationName
找到任何指定角色名称,建议提供程序引发 ProviderException。
如果任何指定的用户名不与配置 applicationName
的任何指定角色名称相关联,我们建议提供程序引发 ProviderException。
如果任何指定的用户名为 null
或为空字符串,建议提供程序引发异常。
如果任何指定的角色名称为 null
或 为空字符串,建议提供程序引发异常。
如果数据源支持事务,建议在事务中包含每个删除操作,并回滚事务,并在任何删除操作失败时引发异常。