public async Task <int> InsertAsync(Department department) { object[] primaryKeyValues = await _repository.InsertAsync(department); int departmentId = (int)primaryKeyValues[0]; department.Id = departmentId; // Add item to the cache list string departmentCacheKey = DepartmentCacheKeys.GetKey(department.Id); await _distributedCache.SetAsync(departmentCacheKey, department); string departmentDetailsCacheKey = DepartmentCacheKeys.GetDetailsKey(department.Id); DepartmentDetailsDto departmentDetailsDto = new DepartmentDetailsDto() { DepartmentId = department.Id, DepartmentName = department.Name, Description = department.Description, IsActive = department.IsActive, CreatedAtUtc = department.CreatedAtUtc, LastModifiedAtUtc = department.LastModifiedAtUtc }; await _distributedCache.SetAsync(departmentDetailsCacheKey, departmentDetailsDto); string departmentListKey = DepartmentCacheKeys.ListKey; await _distributedCache.AddToListAsync(departmentListKey, department, d => d.Name); string departmentSelectListKey = DepartmentCacheKeys.SelectListKey; await _distributedCache.AddToListAsync(departmentSelectListKey, department, d => d.Name); return(departmentId); }
public async Task Authorize(string token, string signalRFingerprint) { if (!IsAuthorized()) { var result = authServ.ValidateToken(token, true); if (result.IsSuccessful) { ClaimsIdentity identity = new ClaimsIdentity(result.Principial.Identity); string fingerprint = identity.FindFirst("Fingerprint").Value; if (fingerprint == signalRFingerprint) { string userId = identity.FindFirst("UserId").Value; string sessionId = identity.FindFirst("SessionId").Value; string ip = Context.GetHttpContext().Connection.RemoteIpAddress.ToString(); Context.Items.Add("Auth", true); Context.Items.Add("UserId", userId); Context.Items.Add("Fingerprint", fingerprint); Context.Items.Add("Ip", ip); Context.Items.Add("SessionId", sessionId); // await Groups.AddToGroupAsync(Context.ConnectionId, userId); SignalRSession session = new SignalRSession() { UserId = Int32.Parse(userId), SessionId = sessionId, Fingerprint = fingerprint, Ip = ip, ConnectionId = Context.ConnectionId, StartedAt = DateTime.Now }; await cache.AddToListAsync(userId, session); } else { logger.LogWarning("Hub auth fail. Fingerprints are not same. Token: " + token); await Clients.Caller.SendAsync("OnHubAuthFalied", "Token doesn't belong this device."); } } else { logger.LogWarning("Hub auth fail. Token: " + token); await Clients.Caller.SendAsync("OnHubAuthFalied", "Invalid token."); } } }