public CreateLinkResult CreateLink(string symbolicName, string userName) { using (var context = new RelayContext()) { var password = _passwordHash.GeneratePassword(_configuration.LinkPasswordLength); var passwordInformation = _passwordHash.CreatePasswordInformation(password); var link = new DbLink { Id = Guid.NewGuid(), Password = passwordInformation.Hash, Salt = passwordInformation.Salt, Iterations = passwordInformation.Iterations, SymbolicName = symbolicName, UserName = userName, CreationDate = DateTime.UtcNow }; context.Links.Add(link); context.SaveChanges(); var result = new CreateLinkResult { Id = link.Id, Password = Convert.ToBase64String(password) }; return(result); } }
public bool Update(Guid id, string password) { if ((id == Guid.Empty) || (String.IsNullOrWhiteSpace(password))) { return(false); } using (var context = new RelayContext()) { var dbUser = context.Users.SingleOrDefault(u => u.Id == id); if (dbUser == null) { return(false); } var passwordInformation = _passwordHash.CreatePasswordInformation(Encoding.UTF8.GetBytes(password)); dbUser.Iterations = passwordInformation.Iterations; dbUser.Salt = passwordInformation.Salt; dbUser.Password = passwordInformation.Hash; context.Entry(dbUser).State = EntityState.Modified; return(context.SaveChanges() == 1); } }
public Guid Create(string userName, string password) { if ((String.IsNullOrWhiteSpace(userName)) || (String.IsNullOrWhiteSpace(password))) { return(Guid.Empty); } var passwordInformation = _passwordHash.CreatePasswordInformation(Encoding.UTF8.GetBytes(password)); var dbUser = new DbUser { Id = Guid.NewGuid(), Iterations = passwordInformation.Iterations, Password = passwordInformation.Hash, Salt = passwordInformation.Salt, UserName = userName, CreationDate = DateTime.Now, }; using (var context = new RelayContext()) { context.Users.Add(dbUser); context.SaveChanges(); return(dbUser.Id); } }
public void DeleteLink(Guid linkId) { _logger?.Verbose("Removing link. link-id={LinkId}", linkId); try { using (var context = new RelayContext()) { var itemToDelete = new DbLink { Id = linkId, }; context.Links.Attach(itemToDelete); context.Links.Remove(itemToDelete); context.SaveChanges(); } } catch (DbUpdateConcurrencyException) { } catch (Exception ex) { _logger?.Error(ex, "Error while removing a link. link-id={LinkId}", linkId); } }
public bool UpdateLink(LinkDetails link) { using (var context = new RelayContext()) { var linkEntity = context.Links.SingleOrDefault(p => p.Id == link.Id); if (linkEntity == null) { return(false); } linkEntity.CreationDate = link.CreationDate; linkEntity.AllowLocalClientRequestsOnly = link.AllowLocalClientRequestsOnly; linkEntity.ForwardOnPremiseTargetErrorResponse = link.ForwardOnPremiseTargetErrorResponse; linkEntity.IsDisabled = link.IsDisabled; linkEntity.MaximumLinks = link.MaximumLinks; linkEntity.SymbolicName = link.SymbolicName; linkEntity.UserName = link.UserName; linkEntity.TokenRefreshWindow = link.TokenRefreshWindow; linkEntity.HeartbeatInterval = link.HeartbeatInterval; linkEntity.ReconnectMinWaitTime = link.ReconnectMinWaitTime; linkEntity.ReconnectMaxWaitTime = link.ReconnectMaxWaitTime; linkEntity.AbsoluteConnectionLifetime = link.AbsoluteConnectionLifetime; linkEntity.SlidingConnectionLifetime = link.SlidingConnectionLifetime; context.Entry(linkEntity).State = EntityState.Modified; return(context.SaveChanges() == 1); } }
public void LogRequest(RequestLogEntry requestLogEntry) { using (var context = new RelayContext()) { var link = new DbLink { Id = requestLogEntry.LinkId }; context.Links.Attach(link); context.RequestLogEntries.Add(new DbRequestLogEntry { Id = Guid.NewGuid(), ContentBytesIn = requestLogEntry.ContentBytesIn, ContentBytesOut = requestLogEntry.ContentBytesOut, OnPremiseConnectorInDate = requestLogEntry.OnPremiseConnectorInDate, OnPremiseConnectorOutDate = requestLogEntry.OnPremiseConnectorOutDate, HttpStatusCode = requestLogEntry.HttpStatusCode, OnPremiseTargetInDate = requestLogEntry.OnPremiseTargetInDate, OnPremiseTargetKey = requestLogEntry.OnPremiseTargetKey, OnPremiseTargetOutDate = requestLogEntry.OnPremiseTargetOutDate, LocalUrl = requestLogEntry.LocalUrl ?? "/", OriginId = requestLogEntry.OriginId, Link = link, LinkId = requestLogEntry.LinkId, }); context.SaveChanges(); } }
private void UnlockUser(RelayContext ctx, DbUser user) { user.LastFailedLoginAttempt = null; user.FailedLoginAttempts = null; ctx.Entry(user).State = EntityState.Modified; ctx.SaveChanges(); _logger?.Information("Unlocking user account {UserName}", user.UserName); }
private void RecordFailedLoginAttempt(RelayContext ctx, DbUser user) { user.LastFailedLoginAttempt = DateTime.UtcNow; user.FailedLoginAttempts = user.FailedLoginAttempts.GetValueOrDefault() + 1; ctx.Entry(user).State = EntityState.Modified; ctx.SaveChanges(); _logger?.Information("User {UserName} failed logging in for {FailedLoginAttempts} attempts", user.UserName, user.FailedLoginAttempts ); }
public void DeleteLink(Guid linkId) { using (var context = new RelayContext()) { var itemToDelete = new DbLink { Id = linkId }; context.Links.Attach(itemToDelete); context.Links.Remove(itemToDelete); context.SaveChanges(); } }
public bool Delete(Guid id) { using (var context = new RelayContext()) { var dbUser = new DbUser { Id = id, }; context.Users.Attach(dbUser); context.Users.Remove(dbUser); return(context.SaveChanges() == 1); } }
public void DeleteAllConnectionsForOrigin(Guid originId) { _logger?.Verbose("Deleting all active connections"); try { using (var context = new RelayContext()) { var invalidConnections = context.ActiveConnections.Where(ac => ac.OriginId == originId).ToList(); context.ActiveConnections.RemoveRange(invalidConnections); context.SaveChanges(); } } catch (Exception ex) { _logger?.Error(ex, "Error during deleting of all active connections"); } }
public bool Disable(Guid id) { using (var context = new RelayContext()) { var dbTraceConfiguration = context.TraceConfigurations.Include(t => t.Link).SingleOrDefault(t => t.Id == id); if (dbTraceConfiguration == null) { return(false); } if (dbTraceConfiguration.EndDate < DateTime.UtcNow) { return(true); } dbTraceConfiguration.EndDate = DateTime.UtcNow; context.Entry(dbTraceConfiguration).State = EntityState.Modified; return(context.SaveChanges() == 1); } }
public bool UpdateLink(Link linkId) { using (var context = new RelayContext()) { var itemToUpdate = context.Links.SingleOrDefault(p => p.Id == linkId.Id); if (itemToUpdate == null) { return(false); } itemToUpdate.CreationDate = linkId.CreationDate; itemToUpdate.AllowLocalClientRequestsOnly = linkId.AllowLocalClientRequestsOnly; itemToUpdate.ForwardOnPremiseTargetErrorResponse = linkId.ForwardOnPremiseTargetErrorResponse; itemToUpdate.IsDisabled = linkId.IsDisabled; itemToUpdate.MaximumLinks = linkId.MaximumLinks; itemToUpdate.SymbolicName = linkId.SymbolicName; itemToUpdate.UserName = linkId.UserName; context.Entry(itemToUpdate).State = EntityState.Modified; return(context.SaveChanges() == 1); } }
public void Create(TraceConfiguration traceConfiguration) { using (var context = new RelayContext()) { var link = new DbLink { Id = traceConfiguration.LinkId }; context.Links.Attach(link); context.TraceConfigurations.Add(new DbTraceConfiguration { CreationDate = DateTime.UtcNow, EndDate = traceConfiguration.EndDate, Id = Guid.NewGuid(), Link = link, LinkId = traceConfiguration.LinkId, StartDate = traceConfiguration.StartDate }); context.SaveChanges(); } }