public async Task <UserAccount> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken) { UserAccount user; using (IDbConnection db = new SqlConnection(_connectionString.GetConnectionString())) { const string sqlQuery = "SELECT * FROM [UriReduction].[User] where UserName=@userName"; user = await db.QuerySingleOrDefaultAsync <UserAccount>(sqlQuery, new { userName = normalizedUserName }); if (user == null) { return(null); } if (user.Id == null) { return(user); } var userRoles = _userRoleRepository.GetAllRoleIdByUserId(user.Id.Value); foreach (var userRole in userRoles) { user.UserRoles.Add( (await _roleStore.FindByIdAsync(userRole.ToString(), new CancellationToken())).Name); } } return(user); }
public async Task <IList <string> > GetRolesAsync(TUser user, CancellationToken cancellationToken) { if (user == null) { throw new ArgumentNullException(nameof(user)); } cancellationToken.ThrowIfCancellationRequested(); var userDb = await ByIdAsync(user.Id, cancellationToken).ConfigureAwait(true); if (userDb == null) { return(new List <string>()); } var roles = new List <string>(); foreach (var item in userDb.Roles) { var dbRole = await _roleStore.FindByIdAsync(item, cancellationToken).ConfigureAwait(true); if (dbRole != null) { roles.Add(dbRole.Name); } } return(roles); }
public async Task RoleStore_create_find_by_id() { var role = _roleTestFactory.CreateRole(); await _roleStore.CreateAsync(role, default(CancellationToken)); await _roleStore.SetNormalizedRoleNameAsync(role, role.Name, default(CancellationToken)); var fetchedRole = await _roleStore.FindByIdAsync(role.Id, default(CancellationToken)); role.Name.ShouldMatch(fetchedRole.Name); }
private async Task SetRoleValueAsync(TUser user, string roleName, bool newValue, CancellationToken cancellationToken) { user.CheckNotNull(nameof(user)); roleName.CheckNotNullOrEmpty(nameof(roleName)); // Validate role name. var role = await _roleStore.FindByIdAsync(roleName, cancellationToken).ConfigureAwait(false); if (role == null) { throw new ArgumentException("roleName value '{0}' is not valid role name.".FormatInvariant(roleName)); } // Add/remove from Ontraport. var contact = new TContact(); contact.SetRole(roleName, newValue); var result = await _ontraportContacts.UpdateAsync(user.Id, contact.GetChanges(), cancellationToken).ConfigureAwait(false); if (result?.Id == null) { throw new InvalidOperationException("Could not update contact with ID {0}".FormatInvariant(user.Id)); } // Add/remove role in memory. lock (user.Roles) { if (newValue && !user.Roles.Contains(role.Id)) { user.Roles.Add(role.Id); } else if (!newValue && user.Roles.Contains(role.Id)) { user.Roles.Remove(role.Id); } } }
public async Task <IList <string> > GetRolesAsync(TUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var userDb = await ById(user.Id); if (userDb == null) { return(new List <string>()); } var roles = new List <string>(); foreach (var item in userDb.Roles) { var dbRole = await _roleStore.FindByIdAsync(item, cancellationToken); if (dbRole != null) { roles.Add(dbRole.Name); } } return(roles); }
public async Task <ApplicationRole> FindByIdAsync(string roleId, CancellationToken cancellationToken) { return(await roleStore.FindByIdAsync(roleId, cancellationToken)); }
public async Task <IdentityRole> FindRoleByIdAsync(string roleId) { return(await _roleStore.FindByIdAsync(roleId, CancellationToken.None)); }