public async Task <ServiceResult> RegistrationAsync(RegistrationViewModel model) { var result = new ServiceResult(); if (await EmailIsUnique(model.Email)) { if (await NicknameIsUnique(model.Nickname)) { User user = CreateUser(model); context.Users.Add(user); await context.SaveChangesAsync(); Token token = CreateToken(user.Id, 0); context.Tokens.Add(token); await context.SaveChangesAsync(); emailServ.SendMessageAsync(user.Email, "Thank you for registration." + $"Click <a href=\"https://{config["AppURL"]}/Account/ConfirmEmail?token={token.Value}\">here</a> " + "to confirm email" , "Confirm email"); } else { result.SetFail("Nickname is already exist"); } } else { result.SetFail("Email is already exist"); } return(result); }
public async Task <ServiceResult> AuthenticateAsync(AuthenticationViewModel model, string ip) { ServiceResult result = new ServiceResult(); User user = await LoginAsync(model); if (user != null) { BannedDevice device = await context.BannedDevices.FirstOrDefaultAsync(d => d.UserId == user.Id && d.Fingerprint == model.Fingerprint); if (device == null) { Session session = await context.Sessions.FirstOrDefaultAsync(s => s.UserId == user.Id && s.FingerPrint == model.Fingerprint); if (session == null) { session = new Session() { UserId = user.Id, SessionId = GenerateRandomToken(), FingerPrint = model.Fingerprint, CreatedAt = DateTime.Now, App = model.App, OsCpu = model.OsCpu, // ExpiresInDays = 30, Location = GetLocationByIp(ip) }; context.Sessions.Add(session); } TokensViewmodel tokens = CreateTokens(new Claim[] { new Claim("UserId", user.Id.ToString()), new Claim("UserEmail", user.Email), new Claim("Fingerprint", model.Fingerprint), new Claim("Ip", ip), new Claim("SessionId", session.SessionId) }); session.IpAddress = ip; session.RefreshToken = tokens.RefreshToken; await context.SaveChangesAsync(); result.Data = JsonConvert.SerializeObject(tokens); } else { result.SetFail("Access is blocked"); } } else { result.SetFail("Authentication failed"); } return(result); }
public async Task CloseSessionAsync(int userId, string sessionId, bool banDevice = false) { Session session = await context.Sessions.FirstOrDefaultAsync(s => s.SessionId == sessionId && s.UserId == userId); if (session != null) { var sessions = await cache.GetListAsync <SignalRSession>(userId.ToString()); SignalRSession signalRSession = sessions.LastOrDefault(s => s.SessionId == session.SessionId); if (signalRSession != null) { await cache.RemoveFromListAsync <SignalRSession>(userId.ToString(), signalRSession); await messHub.Clients.Client(signalRSession.ConnectionId).SendAsync("OnSessionClosed"); } await cache.SetStringAsync("sc:" + session.SessionId, "sessionIsClosed", new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(jwtOptions.Value.ExpiryMinutes) }); if (banDevice) { context.BannedDevices.Add(new BannedDevice() { UserId = userId, Fingerprint = session.FingerPrint }); } context.Sessions.Remove(session); await context.SaveChangesAsync(); } }
private async Task <Dialog> GetOrCreateDialogAsync(int interlocutorId) { Dialog dialog; int[] ids = { userId, interlocutorId }; Array.Sort(ids); dialog = await context.Dialogs.FirstOrDefaultAsync(d => d.FirstUserId == ids[0] && d.SecondUserId == ids[1]); if (dialog == null) { dialog = new Dialog() { FirstUserId = ids[0], SecondUserId = ids[1], IsClosed = false }; context.Dialogs.Add(dialog); await context.SaveChangesAsync(); } return(dialog); }