last ...
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using xCommons.Extensions;
|
||||
@@ -211,6 +212,55 @@ namespace xIds.Providers
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete a Role from Identity store (AspNetRoles) and cascade remove
|
||||
/// all user assignments (AspNetUserRoles).
|
||||
/// </summary>
|
||||
/// <param name="roleName">role name</param>
|
||||
/// <param name="cancellationToken">cancellation token (best-effort; Identity APIs are not cancellable)</param>
|
||||
/// <returns>an instance of <see>IdentityResult</see></returns>
|
||||
public async Task<IdentityResult> DeleteRoleAsync(
|
||||
string roleName,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
if (roleName.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Honor cancellation at the entry point. RoleManager.DeleteAsync does not
|
||||
// accept a CancellationToken, so this is a best-effort check before the call.
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
//
|
||||
// Retrieve the role — throws XException.NotFound if it does not exist
|
||||
var role = await GetRoleAsync(roleName);
|
||||
|
||||
//
|
||||
var usersInRole = await UserManager.GetUsersInRoleAsync(roleName);
|
||||
foreach(var user in usersInRole)
|
||||
{
|
||||
//
|
||||
await UserManager.RemoveFromRoleAsync(
|
||||
user: user,
|
||||
role: roleName
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Persist deletion. EF cascades to AspNetUserRoles automatically
|
||||
// under the default Identity configuration (IdentityUserRole<string>).
|
||||
var result = await RoleManager.DeleteAsync(role);
|
||||
|
||||
//
|
||||
// Return Result ...
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check a User is Adminr not
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user