Beispiel #1
0
        public async Task <IActionResult> LogIn(UserLogInModel loginModel)
        {
            if (ModelState.IsValid)
            {
                var result = await _chatUserService.TryLogIn(
                    loginModel.Name.Trim(),
                    loginModel.Password.Trim());

                if (result.success)
                {
                    var claims = new List <Claim>
                    {
                        new Claim("UserId", result.user.Id.ToString()),
                        new Claim("UserName", result.user.Name)
                    };

                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);

                    var authProperties = new AuthenticationProperties
                    {
                        AllowRefresh = true,
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(20),
                        IsPersistent = true,
                    };

                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal (claimsIdentity),
                        authProperties);

                    _onlineUsers.AddOnlineUser(result.user.Id, result.user.Name);

                    return(RedirectToAction("Index", "Chat"));
                }
            }

            return(View());
        }
Beispiel #2
0
        public async Task <IActionResult> Index()
        {
            var id   = Guid.Parse(HttpContext.User.Claims.Single(x => x.Type == "UserId").Value);
            var name = HttpContext.User.Claims.Single(x => x.Type == "UserName").Value;

            if (!_onlineUsers.IsOnline(id))
            {
                _onlineUsers.AddOnlineUser(id, name);
            }

            var users    = _onlineUsers.List.ToList();
            var messages = await _messageService.GetPublicMessages();

            var conversations = new List <Conversation>
            {
                new Conversation
                {
                    Id       = "Public",
                    Messages = messages.Select(x => new ChatMessageViewModel
                    {
                        ConversationId = "Public",
                        SenderId       = x.SenderId,
                        Sended         = x.When,
                        Received       = x.When,
                        Content        = x.Text
                    }).ToList()
                },
            };

            var vm = new ChatViewModel
            {
                OnlineUsers   = users,
                Conversations = conversations
            };

            return(View(vm));
        }