public async Task UserStoreMethodsThrowWhenDisposedTest() { var db = CreateDb(); var userOnlyStore = new UserOnlyStore(db); var store = new UserStore(db, userOnlyStore); store.Dispose(); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.AddClaimsAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.AddLoginAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.AddToRoleAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetClaimsAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetLoginsAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetRolesAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.IsInRoleAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.RemoveLoginAsync(null, null, null)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.RemoveFromRoleAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.ReplaceClaimAsync(null, null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.FindByLoginAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.FindByIdAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.FindByNameAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.CreateAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.UpdateAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.DeleteAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.SetEmailConfirmedAsync(null, true)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetEmailConfirmedAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.SetPhoneNumberConfirmedAsync(null, true)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.GetPhoneNumberConfirmedAsync(null)); }
public async Task SqlUserStoreMethodsThrowWhenDisposedTest() { var store = new UserStore(new IdentityDbContext (TestEnvironment.Config[TestEnvironment.TestIdentityDbConnectionString])); store.Dispose(); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.AddClaimsAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.AddLoginAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.AddToRoleAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetClaimsAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetLoginsAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetRolesAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.IsInRoleAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.RemoveLoginAsync(null, null, null)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.RemoveFromRoleAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.ReplaceClaimAsync(null, null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.FindByLoginAsync(null, null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.FindByIdAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.FindByNameAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.CreateAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.UpdateAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.DeleteAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.SetEmailConfirmedAsync(null, true)); await Assert.ThrowsAsync <ObjectDisposedException>(async() => await store.GetEmailConfirmedAsync(null)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.SetPhoneNumberConfirmedAsync(null, true)); await Assert.ThrowsAsync <ObjectDisposedException>( async() => await store.GetPhoneNumberConfirmedAsync(null)); }
public IActionResult CreateAdmin([FromBody] UserDto user) { try { if (ModelState.IsValid) { var newUser = new PortfolioIdentityUser { UserName = user.Username, Email = user.EmailAddress, Name = user.Name, Surname = user.Surname, EmailConfirmed = false, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; var roleStore = new RoleStore <IdentityRole>(_context); var adminRole = _context.Roles.FirstOrDefault(r => r.Name == "Admin"); if (!_context.Users.Any(u => u.UserName == newUser.UserName)) { var passwordHasher = new PasswordHasher <PortfolioIdentityUser>(); var hashed = passwordHasher.HashPassword(newUser, user.Password); newUser.PasswordHash = hashed; var userStore = new UserStore <PortfolioIdentityUser>(_context); var claim = new Claim("SuperUser", "True"); userStore.AddToRoleAsync(newUser, "Admin"); userStore.AddClaimsAsync(newUser, new List <Claim> { claim }); _context.Users.Add(newUser); } var result = _context.SaveChangesAsync(); if (result != null) { return(Ok()); } } } catch (Exception ex) { _logger.LogError($"Exception thrown while creating user: {ex}"); } return(BadRequest("Failed to login")); }
private (IdentityRole, IdentityClaim, IdentityExternalLogin, IdentityUser) CreateTestData(IGraphClient client, UserStore <IdentityUser> us, RoleStore <IdentityRole> rs) { var user = CreateUser(client, us); var role = CreateRole(client, rs); us.AddClaimsAsync(user, new[] { new Claim("TEST_CLAIM", "TEST_CLAIM_VALUE") }, default).Wait(); us.AddLoginAsync(user, new UserLoginInfo("GOOGLE", "KEY", "XGOOGLE"), default).Wait(); us.AddToRoleAsync(user, role.Name, default).Wait(); var res = us.UpdateAsync(user, default).Result; Assert.True(res.Succeeded); user = FetchTestUser(client, us); return(role, user.Claims.FirstOrDefault(), user.Logins.FirstOrDefault(), user); }
public void Seed() { var user = new PortfolioIdentityUser { UserName = "******", Email = "*****@*****.**", Name = "Pieter", Surname = "Myburgh", EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; var roleStore = new RoleStore <IdentityRole>(_context); IdentityRole adminRole = _context.Roles.FirstOrDefault(r => r.Name == "Admin"); if (adminRole == null) { adminRole = new IdentityRole { Name = "Admin", NormalizedName = "Admin" }; _context.Roles.Add(adminRole); } if (!_context.Users.Any(u => u.UserName == user.UserName)) { var password = new PasswordHasher <PortfolioIdentityUser>(); var hashed = password.HashPassword(user, "P@m180584"); user.PasswordHash = hashed; var userStore = new UserStore <PortfolioIdentityUser>(_context); var claim = new Claim("SuperUser", "True"); userStore.AddToRoleAsync(user, "Admin"); userStore.AddClaimsAsync(user, new List <Claim> { claim }); _context.Users.Add(user); } _context.SaveChanges(); }
protected static async Task CreateUser(TContext context, TUser user) { using var userStore = new UserStore <TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRoleClaim>(context); using var roleStore = new RoleStore <TRole, TContext, TKey, TUserRole, TRoleClaim>(context); await userStore.CreateAsync(user); await userStore.AddClaimsAsync(user, new[] { new Claim("T1", "V1"), new Claim("T1", "V2"), new Claim("T2", "V3") }); var adminRole = new TRole { NormalizedName = "admin", Name = "Admin" }; await roleStore.CreateAsync(adminRole); await userStore.AddToRoleAsync(user, "admin"); await roleStore.AddClaimAsync(adminRole, new Claim("AC1", "V1")); await roleStore.AddClaimAsync(adminRole, new Claim("AC2", "V1")); var moderatorRole = new TRole { NormalizedName = "moderator", Name = "Moderator" }; await roleStore.CreateAsync(moderatorRole); await userStore.AddToRoleAsync(user, "moderator"); await roleStore.AddClaimAsync(moderatorRole, new Claim("MC1", "V1")); await roleStore.AddClaimAsync(moderatorRole, new Claim("MC2", "V1")); await userStore.AddLoginAsync(user, new UserLoginInfo("ISCABBS", "DrDave", "SSHFTW")); await userStore.AddLoginAsync(user, new UserLoginInfo("Local", "EekyBear", "PPS")); await userStore.SetTokenAsync(user, "ISCABBS", "DrDave", "SSHFTW", CancellationToken.None); await context.SaveChangesAsync(); }
public Task AddClaimsAsync(IUser user, IEnumerable <Claim> claims, CancellationToken cancellationToken) { return(innerStore.AddClaimsAsync((WrappedIdentityUser)user, claims, cancellationToken)); }
public async Task AddClaimsAsync([FromBody] AddClaimDTO claimInfo, [FromServices] WebStoreDB dB) { await _userStore.AddClaimsAsync(claimInfo.User, claimInfo.Claims); await dB.SaveChangesAsync(); }
public async Task AddClaimsAsync([FromBody] AddClaimDto ClaimInfo, [FromServices] WebStore_StudyDb db) { await userStore.AddClaimsAsync(ClaimInfo.User, ClaimInfo.Claims); await db.SaveChangesAsync(); }
public async Task AddClaimsAsync([FromBody] AddClaimsDto claimsDto) { await _userStore.AddClaimsAsync(claimsDto.User, claimsDto.Claims); }
public void Initialize() { var adminUser = new ApplicationUser { Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", UserName = "******", NormalizedUserName = "******", EmailConfirmed = true, SecurityStamp = Guid.NewGuid().ToString("D") }; var botUser = new ApplicationUser { Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", UserName = "******", NormalizedUserName = "******", EmailConfirmed = true, SecurityStamp = Guid.NewGuid().ToString("D") }; if (!_context.Users.Any(u => u.UserName == adminUser.UserName)) { var password = new PasswordHasher <ApplicationUser>(); var hashed = password.HashPassword(adminUser, "Admin12!"); adminUser.PasswordHash = hashed; var userStore = new UserStore <ApplicationUser>(_context); var result = userStore.CreateAsync(adminUser); var claims = new List <Claim> { new Claim(ClaimTypes.Role, "Manager"), new Claim(ClaimTypes.NameIdentifier, adminUser.Id) }; userStore.AddClaimsAsync(adminUser, claims); } if (!_context.Users.Any(u => u.UserName == botUser.UserName)) { var password = new PasswordHasher <ApplicationUser>(); var hashed = password.HashPassword(botUser, "Bot1234!"); botUser.PasswordHash = hashed; var userStore = new UserStore <ApplicationUser>(_context); var result = userStore.CreateAsync(botUser); var claims = new List <Claim> { new Claim(ClaimTypes.Role, "Bot"), new Claim(ClaimTypes.NameIdentifier, botUser.Id) }; userStore.AddClaimsAsync(botUser, claims); BotExtentions.BotId = botUser.Id; } else { var bot = _context.Users.FirstOrDefault(x => x.NormalizedUserName == "BOT"); BotExtentions.BotId = bot.Id; } _context.SaveChanges(); }
public async Task AddClaimsAsync([FromBody] AddClaimsDto ClaimInfo) { await _UserStore.AddClaimsAsync(ClaimInfo.User, ClaimInfo.Claims); }
public void ArgumentNullExceptions() { // Should throw ArgumentNullException Assert.ThrowsAsync <ArgumentNullException>(() => _store.GetClaimsAsync(null, CancellationToken.None)); Assert.ThrowsAsync <ArgumentNullException>(() => _store.AddClaimsAsync(null, new Claim[] { new Claim("x", "y") }, CancellationToken.None)); Assert.ThrowsAsync <ArgumentNullException>(() => _store.AddClaimsAsync(_user, null, CancellationToken.None)); Assert.ThrowsAsync <ArgumentNullException>(() => _store.RemoveClaimsAsync(null, new Claim[] { new Claim("x", "y") }, CancellationToken.None)); Assert.ThrowsAsync <ArgumentNullException>(() => _store.RemoveClaimsAsync(_user, null, CancellationToken.None)); Assert.ThrowsAsync <ArgumentNullException>(() => _store.ReplaceClaimAsync(null, new Claim("x", "y"), new Claim("a", "b"), CancellationToken.None)); Assert.ThrowsAsync <ArgumentNullException>(() => _store.ReplaceClaimAsync(_user, null, new Claim("a", "b"), CancellationToken.None)); Assert.ThrowsAsync <ArgumentNullException>(() => _store.ReplaceClaimAsync(_user, new Claim("x", "y"), null, CancellationToken.None)); }
public async Task AddClaimsAsync([FromBody] AddClaimDTO ClaimInfo) { await _UserStore.AddClaimsAsync(ClaimInfo.User, ClaimInfo.Claims); await _UserStore.Context.SaveChangesAsync(); }
public void AddRemoveUserClaim() { UserStore <ApplicationUser, IdentityRole, IdentityCloudContext> store = CreateUserStore(); UserManager <ApplicationUser> manager = CreateUserManager(); var user = CurrentUser; WriteLineObject <IdentityUser>(user); Claim claim = GenAdminClaim(); var userClaimTask = manager.AddClaimAsync(user, claim); userClaimTask.Wait(); Assert.IsTrue(userClaimTask.Result.Succeeded, string.Concat(userClaimTask.Result.Errors)); var claimsTask = manager.GetClaimsAsync(user); claimsTask.Wait(); Assert.IsTrue(claimsTask.Result.Any(c => c.Value == claim.Value & c.ValueType == claim.ValueType), "Claim not found"); var userRemoveClaimTask = manager.RemoveClaimAsync(user, claim); userRemoveClaimTask.Wait(); Assert.IsTrue(userClaimTask.Result.Succeeded, string.Concat(userClaimTask.Result.Errors)); var claimsTask2 = manager.GetClaimsAsync(user); claimsTask2.Wait(); Assert.IsTrue(!claimsTask2.Result.Any(c => c.Value == claim.Value & c.ValueType == claim.ValueType), "Claim not removed"); //adding test for removing an empty claim Claim claimEmpty = GenAdminClaimEmptyValue(); var userClaimTask2 = manager.AddClaimAsync(user, claimEmpty); userClaimTask2.Wait(); var userRemoveClaimTask2 = manager.RemoveClaimAsync(user, claimEmpty); userRemoveClaimTask2.Wait(); Assert.IsTrue(userClaimTask2.Result.Succeeded, string.Concat(userClaimTask2.Result.Errors)); Assert.ThrowsException <ArgumentNullException>(() => store.AddClaimsAsync(null, new List <Claim>() { claim }).Wait()); Assert.ThrowsException <ArgumentNullException>(() => store.AddClaimsAsync(user, null).Wait()); Assert.IsTrue(Assert.ThrowsException <AggregateException>(() => store.RemoveClaimsAsync(null, new List <Claim>() { claim }).Wait()).InnerException is ArgumentException); Assert.IsTrue(Assert.ThrowsException <AggregateException>(() => store.RemoveClaimsAsync(user, null).Wait()).InnerException is ArgumentException); Assert.ThrowsException <ArgumentNullException>(() => store.RemoveClaimsAsync(user, new List <Claim>() { new Claim(claim.Type, null) }).Wait()); Assert.ThrowsException <ArgumentNullException>(() => store.AddClaimsAsync(null, new List <Claim>() { claim }).Wait()); }
public async Task AddClaimsAsync([FromBody] AddClaimDTO claimInfo) => await _userStore.AddClaimsAsync(claimInfo.User, claimInfo.Claims);
public async Task AddRemoveUserClaim(bool includeRoles) { UserStore <ApplicationUser, IdentityRole, IdentityCloudContext> store = CreateUserStore(includeRoles); UserManager <ApplicationUser> manager = CreateUserManager(includeRoles); var user = await CurrentUser(includeRoles); WriteLineObject <IdentityUser>(user); Claim claim = GenAdminClaim(); var userClaimTask = await manager.AddClaimAsync(user, claim); Assert.IsTrue(userClaimTask.Succeeded, string.Concat(userClaimTask.Errors)); var claimsTask = await manager.GetClaimsAsync(user); Assert.IsTrue(claimsTask.Any(c => c.Value == claim.Value & c.ValueType == claim.ValueType), "Claim not found"); var userRemoveClaimTask = await manager.RemoveClaimAsync(user, claim); Assert.IsTrue(userClaimTask.Succeeded, string.Concat(userClaimTask.Errors)); var claimsTask2 = await manager.GetClaimsAsync(user); Assert.IsTrue(!claimsTask2.Any(c => c.Value == claim.Value & c.ValueType == claim.ValueType), "Claim not removed"); //adding test for removing an empty claim Claim claimEmpty = GenAdminClaimEmptyValue(); var userClaimTask2 = await manager.AddClaimAsync(user, claimEmpty); var userRemoveClaimTask2 = await manager.RemoveClaimAsync(user, claimEmpty); Assert.IsTrue(userClaimTask2.Succeeded, string.Concat(userClaimTask2.Errors)); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await store.AddClaimsAsync(null, new List <Claim>() { claim })); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await store.AddClaimsAsync(user, null)); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await store.RemoveClaimsAsync(null, new List <Claim>() { claim })); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await store.RemoveClaimsAsync(user, null)); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await store.RemoveClaimsAsync(user, new List <Claim>() { new Claim(claim.Type, null) })); await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await store.AddClaimsAsync(null, new List <Claim>() { claim })); }
public async Task UserStorePublicNullCheckTest() { Assert.Throws <ArgumentNullException>("factory", () => new UserStore <IdentityUser>(null)); var store = new UserStore <IdentityUser>(CreateTestContext()); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetUserIdAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetUserNameAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetUserNameAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.CreateAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.UpdateAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.DeleteAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.AddClaimsAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.ReplaceClaimAsync(null, null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.RemoveClaimsAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetClaimsAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetLoginsAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetRolesAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.AddLoginAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.RemoveLoginAsync(null, null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.AddToRoleAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.RemoveFromRoleAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.IsInRoleAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetPasswordHashAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetPasswordHashAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetSecurityStampAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetSecurityStampAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("login", async() => await store.AddLoginAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync <ArgumentNullException>("claims", async() => await store.AddClaimsAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync <ArgumentNullException>("claims", async() => await store.RemoveClaimsAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetEmailConfirmedAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetEmailConfirmedAsync(null, true)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetEmailAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetEmailAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetPhoneNumberAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetPhoneNumberAsync(null, null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetPhoneNumberConfirmedAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetPhoneNumberConfirmedAsync(null, true)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetTwoFactorEnabledAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetTwoFactorEnabledAsync(null, true)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetAccessFailedCountAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetLockoutEnabledAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetLockoutEnabledAsync(null, false)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.GetLockoutEndDateAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.SetLockoutEndDateAsync(null, new DateTimeOffset())); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.ResetAccessFailedCountAsync(null)); await Assert.ThrowsAsync <ArgumentNullException>("user", async() => await store.IncrementAccessFailedCountAsync(null)); await Assert.ThrowsAsync <ArgumentException>("normalizedRoleName", async() => await store.AddToRoleAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync <ArgumentException>("normalizedRoleName", async() => await store.RemoveFromRoleAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync <ArgumentException>("normalizedRoleName", async() => await store.IsInRoleAsync(new IdentityUser("fake"), null)); await Assert.ThrowsAsync <ArgumentException>("normalizedRoleName", async() => await store.AddToRoleAsync(new IdentityUser("fake"), "")); await Assert.ThrowsAsync <ArgumentException>("normalizedRoleName", async() => await store.RemoveFromRoleAsync(new IdentityUser("fake"), "")); await Assert.ThrowsAsync <ArgumentException>("normalizedRoleName", async() => await store.IsInRoleAsync(new IdentityUser("fake"), "")); }
public async Task _02_CanDoCurd() { var user = new NHIdentityUser { UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", PhoneNumber = "02000000000", PhoneNumberConfirmed = true, LockoutEnabled = false, LockoutEnd = null, AccessFailedCount = 0, NormalizedEmail = "*****@*****.**", PasswordHash = null, SecurityStamp = null }; var result = await store.CreateAsync(user); Assert.True(result.Succeeded); var id = user.Id; Assert.IsNotEmpty(id); Assert.IsNotEmpty(user.ConcurrencyStamp); user.LockoutEnabled = true; user.LockoutEnd = DateTimeOffset.UtcNow.AddMinutes(20); result = await store.UpdateAsync(user); Assert.True(result.Succeeded); var lockouts = await store.Users .Where(u => u.LockoutEnabled) .CountAsync(); Assert.True(lockouts > 0); user = await store.FindByEmailAsync(user.NormalizedEmail); Assert.True(user.Id == id); user = await store.FindByNameAsync(user.NormalizedUserName); Assert.True(user.Id == id); user = await store.FindByIdAsync(id); Assert.True(user.Id == id); var claim = new Claim("Test", Guid.NewGuid().ToString("N")); await store.AddClaimsAsync(user, new [] { claim }); var claims = await store.GetClaimsAsync(user); Assert.True(claims.Count > 0); var users = await store.GetUsersForClaimAsync(claim); Assert.IsNotEmpty(users); await store.RemoveClaimsAsync(user, claims); var loginInfo = new Microsoft.AspNetCore.Identity.UserLoginInfo( "test", Guid.NewGuid().ToString("N"), "Test" ); await store.AddLoginAsync(user, loginInfo); await store.SetTokenAsync( user, loginInfo.LoginProvider, loginInfo.ProviderDisplayName, loginInfo.ProviderKey, CancellationToken.None ); await store.RemoveTokenAsync( user, loginInfo.LoginProvider, loginInfo.ProviderDisplayName, CancellationToken.None ); await store.RemoveLoginAsync( user, loginInfo.LoginProvider, loginInfo.ProviderKey ); result = await store.DeleteAsync(user); Assert.True(result.Succeeded); }
public async Task AddClaimsAsync([FromBody] AddClaimDTO ClaimInfo, [FromServices] WebStoreDB db) { await _UserStore.AddClaimsAsync(ClaimInfo.User, ClaimInfo.Claims); await db.SaveChangesAsync(); }
public async Task AddClaimsAsync([FromBody] AddClaimDTO claimDto) { await _userStore.AddClaimsAsync(claimDto.User, claimDto.Claims); await _userStore.Context.SaveChangesAsync(); }