public async Task SignInAsync(ISignInRoomDTO dto) { var room = _roomRepository.GetAll(r => r.UniqName == dto.UniqName).FirstOrDefault(); if (room == null) { throw new RoomNotFoundException(); } if (room.UserRooms.Any(ur => ur.User.NormalizedUserName == _currentUserName)) { AddRoomToUserIdentity(dto.UniqName); return; } if (room.PasswordHash != HashPasswordHelper.GetPasswordHash(dto.Password)) { throw new IncorrectPasswordException(); } var user = await _userManager.FindByNameAsync(_currentUserName); room.UserRooms.Add(new UserRoom() { UserId = user.Id, RoomId = room.Id }); _roomRepository.Update(room); AddRoomToUserIdentity(dto.UniqName); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { #region Relationships modelBuilder.Entity <Room>() .HasOne(r => r.PlayList) .WithOne(pl => pl.Room) .HasForeignKey <PlayList>(pl => pl.RoomId); modelBuilder.Entity <Room>() .HasMany(r => r.Messages) .WithOne(m => m.Room) .HasForeignKey(m => m.RoomId); modelBuilder.Entity <UserRoom>() .HasKey(ur => new { ur.RoomId, ur.UserId }); modelBuilder.Entity <UserRoom>() .HasOne(ur => ur.Room) .WithMany(r => r.UserRooms) .HasForeignKey(ur => ur.RoomId); modelBuilder.Entity <UserRoom>() .HasOne(ur => ur.User) .WithMany(u => u.UserRooms) .HasForeignKey(ur => ur.UserId); #endregion #region DataSeeding var user = new User() { Id = 1, UserName = "******", NormalizedUserName = "******".ToUpper() }; var user2 = new User() { Id = 2, UserName = "******", NormalizedUserName = "******".ToUpper() }; user.PasswordHash = new PasswordHasher <User>().HashPassword(user, "Password1"); user2.PasswordHash = new PasswordHasher <User>().HashPassword(user2, "Password1"); modelBuilder.Entity <User>() .HasData(user, user2); modelBuilder.Entity <IdentityRole <long> >() .HasData(new IdentityRole <long> { Id = 1, Name = "Admin", NormalizedName = "Admin".ToUpper() }); modelBuilder.Entity <UserRoom>() .HasData(new UserRoom() { RoomId = 1, UserId = 1 }); modelBuilder.Entity <Message>() .HasData(new Message() { Id = 1, DateSent = DateTime.UtcNow, RoomId = 1, UserId = 1, HashMessage = "SomeMessage" }); modelBuilder.Entity <Message>() .Property(m => m.DateSent) .HasDefaultValueSql("getutcdate()"); modelBuilder.Entity <Room>() .HasData(new Room() { Id = 1, Name = "Room1", UniqName = "UniqRoomNameAzaza", PasswordHash = HashPasswordHelper.GetPasswordHash("Password1") }); modelBuilder.Entity <PlayList>() .HasData(new PlayList() { Id = 1, RoomId = 1, TrackCurrentTime = new TimeSpan() }); #endregion base.OnModelCreating(modelBuilder); }
public RoomMappingProfile() { CreateMap <IAddRoomDTO, Room>() .ForMember(r => r.PasswordHash, opt => opt.MapFrom(dto => HashPasswordHelper.GetPasswordHash(dto.Password))); }