public async Task <IdentityResult> CreateAsync(AspNetMembershipRole role, CancellationToken cancellationToken) { try { var dbRole = Convert(role); _dbcontext.AspNetRoles.Add(dbRole); await _dbcontext.SaveChangesAsync(cancellationToken); return(IdentityResult.Success); } catch (Exception ex) { return(IdentityResult.Failed(new IdentityError { Code = ex.GetType().Name, Description = ex.Message })); } }
public async Task AddToRoleAsync(AspNetMembershipUser user, string roleName, CancellationToken cancellationToken) { var dbuser = await _dbcontext.AspNetUsers .Where(u => u.UserId == user.Id) .SingleOrDefaultAsync(cancellationToken) .ConfigureAwait(false); var role = await _dbcontext.AspNetRoles .Where(r => r.LoweredRoleName == roleName.ToLower()) .SingleOrDefaultAsync(cancellationToken) .ConfigureAwait(false); var userRole = new AspNetUsersInRoles { RoleId = role.RoleId, UserId = dbuser.UserId }; await _dbcontext.AspNetUsersInRoles.AddAsync(userRole, cancellationToken).ConfigureAwait(false); await _dbcontext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); }