public async Task <IActionResult> Login(Guid chatRoomId) { try { Guid userId = Guid.NewGuid(); var user = await _context.ChatRoomParticipants.FindAsync(userId); if (user == null) { var room = await _context.ChatRooms .Where(r => r.Id == chatRoomId) .SingleOrDefaultAsync(); if (room == null) { room = new ChatRoom { Id = chatRoomId, Name = $"{chatRoomId}" }; } user = new ChatRoomParticipant { Id = userId, ChatRoom = room, Name = Request.Headers["User-Agent"] }; _context.ChatRoomParticipants.Add(user); await _context.SaveChangesAsync(); } if (user == null) { throw new Exception($"User with ID {userId} not found"); } var claims = User.Claims .Where(x => x.Type != $"Room{chatRoomId}") .Concat(new[] { new Claim($"Room{chatRoomId}", userId.ToString()) }) .ToList(); var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity)); return(RedirectToAction("Index", "Chat", new { chatRoomId })); } catch (Exception ex) { _logger.LogError(ex, "Could not log user in"); return(StatusCode(500)); } }
public static void Run() { Chatroom chatroom = new Chatroom(); var Shalom = new ChatRoomParticipant("Shalom"); var David = new ChatRoomParticipant("David"); var King = new ChatRoomParticipant("King"); var Jam = new ChatRoomParticipant("Raspberry Jam"); chatroom.Register(Shalom); chatroom.Register(David); chatroom.Register(King); chatroom.Register(Jam); //Chat functionality Shalom.Send(David.ID, "Hi David!"); David.Send(Shalom.ID, "Hi there!"); King.Send(Jam.ID, "Want to eat some pancakes?"); Jam.Send(King.ID, "Yep!"); // Wait for user Console.ReadKey(); }
public async Task <ActionResult <ExistingChatRoomParticipant> > PostChatRoomParticipant(Guid roomId, ChatRoomParticipant newProperties) { var chatRoomParticipant = new Models.ChatRoomParticipant { ChatRoomId = roomId, Name = newProperties.Name }; _context.ChatRoomParticipants.Add(chatRoomParticipant); await _context.SaveChangesAsync(); return(CreatedAtAction("GetChatRoomParticipant", new { roomId, id = chatRoomParticipant.Id }, chatRoomParticipant)); }
public async Task <IActionResult> PutChatRoomParticipant(Guid roomId, Guid id, ChatRoomParticipant newProperties) { var chatRoomParticipant = await _context.ChatRoomParticipants .Where(p => p.ChatRoomId == roomId) .Where(p => p.Id == id) .SingleOrDefaultAsync(); if (chatRoomParticipant == null) { return(NotFound()); } chatRoomParticipant.Name = newProperties.Name; await _context.SaveChangesAsync(); return(NoContent()); }