public override async Task RemoveFromRoleAsync(ApplicationUser <TKey> user, string normalizedRoleName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); if (string.IsNullOrEmpty(normalizedRoleName)) { throw new ArgumentException(nameof(normalizedRoleName)); } var roleEntity = await FindRoleAsync(normalizedRoleName, cancellationToken); if (roleEntity != null) { var userRoles = (await UserRolesTable.GetRolesAsync(user.Id))?.Select(x => new IdentityUserRole <TKey> { UserId = user.Id, RoleId = x.Id }).ToList(); UserRoles = userRoles; var userRole = await FindUserRoleAsync(user.Id, roleEntity.Id, cancellationToken); if (userRole != null) { UserRoles.Remove(userRole); } } }
public override async Task AddToRoleAsync(ApplicationUser <TKey> user, string normalizedRoleName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); if (string.IsNullOrEmpty(normalizedRoleName)) { throw new ArgumentException($"Parameter {nameof(normalizedRoleName)} cannot be null or empty."); } var role = await FindRoleAsync(normalizedRoleName, cancellationToken); if (role == null) { throw new InvalidOperationException($"Role '{normalizedRoleName}' was not found."); } var userRoles = (await UserRolesTable.GetRolesAsync(user.Id))?.Select(x => new IdentityUserRole <TKey> { UserId = user.Id, RoleId = x.Id }).ToList(); UserRoles = userRoles; UserRoles.Add(CreateUserRole(user, role)); }
public override async Task <IdentityResult> DeleteAsync(ApplicationUser <TKey> user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); return(await UsersTable.DeleteAsync(user)); }
public override async Task AddLoginAsync(ApplicationUser <TKey> user, UserLoginInfo login, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); login.ThrowIfNull(nameof(login)); UserLogins ??= (await UserLoginsTable.GetLoginsAsync(user.Id)).ToList(); UserLogins.Add(CreateUserLogin(user, login)); }
public override async Task <IList <string> > GetRolesAsync(ApplicationUser <TKey> user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); var userLogins = await UserRolesTable.GetRolesAsync(user.Id); return(userLogins.Select(x => x.Name).ToList()); }
public override async Task <IList <UserLoginInfo> > GetLoginsAsync(ApplicationUser <TKey> user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); var userLogins = await UserLoginsTable.GetLoginsAsync(user.Id); return(userLogins.Select(x => new UserLoginInfo(x.LoginProvider, x.LoginProvider, x.ProviderDisplayName)).ToList()); }
public override async Task <IList <Claim> > GetClaimsAsync(ApplicationUser <TKey> user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); var userClaims = await UserClaimsTable.GetClaimsAsync(user.Id); return(userClaims.Select(x => new Claim(x.ClaimType, x.ClaimValue)).ToList()); }
public override async Task <IdentityResult> UpdateAsync(ApplicationUser <TKey> user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); user.ThrowIfNull(nameof(user)); user.ConcurrencyStamp = Guid.NewGuid().ToString(); var update = await UsersTable.UpdateAsync(user, UserClaims, UserRoles, UserLogins, UserTokens); return(update ? IdentityResult.Success : IdentityResult.Failed(new IdentityError { Description = $"User '{user.UserName}' could not be deleted" })); }
public override async Task AddClaimsAsync(ApplicationUser <TKey> user, IEnumerable <Claim> claims, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); claims.ThrowIfNull(nameof(claims)); UserClaims ??= (await UserClaimsTable.GetClaimsAsync(user.Id)).ToList(); foreach (var claim in claims) { UserClaims.Add(CreateUserClaim(user, claim)); } }
public override async Task RemoveLoginAsync(ApplicationUser <TKey> user, string loginProvider, string providerKey, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); UserLogins ??= (await UserLoginsTable.GetLoginsAsync(user.Id)).ToList(); var userLogin = await FindUserLoginAsync(user.Id, loginProvider, providerKey, cancellationToken); if (userLogin != null) { UserLogins.Remove(userLogin); } }
public override async Task ReplaceClaimAsync(ApplicationUser <TKey> user, Claim claim, Claim newClaim, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); user.ThrowIfNull(nameof(user)); claim.ThrowIfNull(nameof(claim)); newClaim.ThrowIfNull(nameof(newClaim)); UserClaims ??= (await UserClaimsTable.GetClaimsAsync(user.Id)).ToList(); var matchedClaims = UserClaims.Where(x => x.UserId.Equals(user.Id) && x.ClaimType == claim.Type && x.ClaimValue == claim.Value); foreach (var matchedClaim in matchedClaims) { matchedClaim.ClaimType = newClaim.Type; matchedClaim.ClaimValue = newClaim.Value; } }
public override async Task RemoveClaimsAsync(ApplicationUser <TKey> user, IEnumerable <Claim> claims, CancellationToken cancellationToken) { ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); claims.ThrowIfNull(nameof(claims)); UserClaims ??= (await UserClaimsTable.GetClaimsAsync(user.Id)).ToList(); foreach (var claim in UserClaims) { var matchedClaims = UserClaims.Where(x => x.UserId.Equals(user.Id) && x.ClaimType == claim.ClaimType && x.ClaimValue == claim.ClaimValue); foreach (var matchedClaim in matchedClaims) { UserClaims.Remove(matchedClaim); } } }
public override async Task <bool> IsInRoleAsync(ApplicationUser <TKey> user, string normalizedRoleName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); if (string.IsNullOrEmpty(normalizedRoleName)) { throw new ArgumentException($"Parameter {nameof(normalizedRoleName)} cannot be null or empty."); } var role = await FindRoleAsync(normalizedRoleName, cancellationToken); if (role != null) { var userRole = await FindUserRoleAsync(user.Id, role.Id, cancellationToken); return(userRole != null); } return(false); }