public async Task MigrateIdentityDbAsync() { try { if (!_persistedGrantContext.AllMigrationsApplied()) { _logger.LogInformation("Running PersistedGrantDbContext Migration"); await _persistedGrantContext.Database.MigrateAsync(); await _persistedGrantContext.SaveChangesAsync(); } if (!_configurationContext.AllMigrationsApplied()) { _logger.LogInformation("Running ConfigurationDbContext Migration"); await _configurationContext.Database.MigrateAsync(); await _configurationContext.SaveChangesAsync(); } if (!_identityContext.AllMigrationsApplied()) { _logger.LogInformation("Running IdentityDbContext Migration"); await _identityContext.Database.MigrateAsync(); await _identityContext.SaveChangesAsync(); } } catch (Exception ex) { string message = LoggingEvents.MigratingDatabase.Name + " em-identitydb"; _logger.LogCritical(LoggingEvents.MigratingDatabase, ex, message); throw new Exception(message, ex); } }
public async Task DeleteAllBySubjectIdAsync_Should_Delete_All_PersistedGrants_For_Particular_SubjectId() { var subjectId = Guid.NewGuid(); var persistedGrant = new PersistedGrant { ClientId = "clientId", Type = "refresh_token", Data = "{\"CreationTime\":\"2020-02-17T18:39:53Z\",\"Lifetime\":2592000,\"AccessToken\":{\"Audiences\":[\"Riva.Identity\"],\"Issuer\":\"http://*****:*****@gmail.com\",\"ValueType\":\"http://www.w3.org/2001/XMLSchema#string\"},{\"Type\":\"email_verified\",\"Value\":\"True\",\"ValueType\":\"http://www.w3.org/2001/XMLSchema#boolean\"},{\"Type\":\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name\",\"Value\":\"[email protected]\",\"ValueType\":\"http://www.w3.org/2001/XMLSchema#string\"},{\"Type\":\"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier\",\"Value\":\"5eb19001-41ef-4819-9a96-e7f777fe3dcc\",\"ValueType\":\"http://www.w3.org/2001/XMLSchema#string\"}],\"Version\":4},\"Version\":4}", CreationTime = DateTime.UtcNow, Key = "LUzQkz9A1uljITj7GRk4FkwFXA7z6S8VN8Cx1EnWxg8=", SubjectId = subjectId.ToString(), Expiration = DateTime.UtcNow.AddDays(1) }; _context.PersistedGrants.Add(persistedGrant); await _context.SaveChangesAsync(); Func <Task> result = async() => await _persistedGrantRepository.DeleteAllBySubjectIdAsync(subjectId); await result.Should().NotThrowAsync <Exception>(); var deletedEntity = await _context.PersistedGrants.FindAsync(persistedGrant.Key); deletedEntity.Should().BeNull(); }
public async Task ValidateAsync(ExtensionGrantValidationContext context) { string phone = context.Request.Raw["phone"]; string code = context.Request.Raw["code"]; var ErrorGrantValidationResult = new GrantValidationResult(TokenRequestErrors.InvalidGrant); if (string.IsNullOrWhiteSpace(phone) || string.IsNullOrWhiteSpace(code)) { context.Result = ErrorGrantValidationResult; return; } var student = await _UserService.Student_LoginBySms(phone, code); if (student == null) { context.Result = ErrorGrantValidationResult; return; } var claims = new List <Claim>() { new Claim("role", "student") }; var tokenlist = _persistedGrantDbContext.PersistedGrants.Where(vn => vn.SubjectId == student.ID).ToList(); _persistedGrantDbContext.PersistedGrants.RemoveRange(tokenlist); await _persistedGrantDbContext.SaveChangesAsync(); context.Result = new GrantValidationResult(student.ID, GrantType, claims); }
public async Task DeleteAllBySubjectIdAsync(Guid subjectId) { var entities = await _persistedGrantDbContext.PersistedGrants .Where(x => x.SubjectId.Equals(subjectId.ToString())) .ToListAsync(); _persistedGrantDbContext.RemoveRange(entities); await _persistedGrantDbContext.SaveChangesAsync(); }
public Task AdddPersistedGrant([FromQuery] string subid, [FromQuery] string clientid) { persistedGrantDbContext.PersistedGrants.Add(new IdentityServer4.EntityFramework.Entities.PersistedGrant { Key = Guid.NewGuid().ToString(), Type = "sub", SubjectId = subid, ClientId = clientid, CreationTime = DateTime.Now, Expiration = DateTime.Now, Data = "haha" }); return(persistedGrantDbContext.SaveChangesAsync()); }