コード例 #1
0
        public async Task <IActionResult> Login(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var findUser = await _context.Users.FirstOrDefaultAsync(t => t.Email.Equals(model.Email));

            var user = new User()
            {
                Id            = findUser.Id.ToString(),
                UserName      = findUser.UserName,
                Email         = findUser.Email,
                Avatar        = findUser.Avatar,
                TopicsCount   = findUser.TopicsCount,
                CommentsCount = findUser.CommentsCount,
                LastLoginDate = findUser.LastLoginDate,
                RegisterDate  = findUser.RegisterDate
            };

            var result = await _signInManager.PasswordSignInAsync(user.UserName, model.Password, false, lockoutOnFailure : false);

            if (result.Succeeded)
            {
                var roles = await _userManager.GetRolesAsync(findUser);

                user.LastLoginDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm");

                _context.Update(findUser);
                await _context.SaveChangesAsync();

                await _signInManager.SignOutAsync();

                var token = new JwtBuilder()
                            .WithAlgorithm(new HMACSHA256Algorithm())
                            .WithSecret(Encoding.ASCII.GetBytes("Ta aplikacja jest turbo fajna")) //ignore this string
                            .AddClaim("exp", DateTimeOffset.UtcNow.AddMinutes(10).ToUnixTimeSeconds())
                            .AddClaim("id", user.Id.ToString())
                            .AddClaim("username", user.UserName)
                            .Encode();

                return(Ok(new
                {
                    token,
                    user,
                    roles
                }));
            }

            return(BadRequest());
        }
コード例 #2
0
        public async Task <IActionResult> AddTopic(AddTopicViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            User user = await _context.User.FirstOrDefaultAsync(t => t.Id.Equals(model.UserId));

            Topic topic = new Topic()
            {
                Title = model.Topic,
                Rate  = 0,
                Date  = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                User  = user
            };

            Comment comment = new Comment()
            {
                Content = model.Comment,
                Date    = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                User    = user,
                Topic   = topic
            };

            user.TopicsCount++;
            user.CommentsCount++;

            _context.Update(user);
            await _context.AddAsync(topic);

            await _context.AddAsync(comment);

            await _context.SaveChangesAsync();

            return(Ok());
        }
コード例 #3
0
        public async Task <IActionResult> AddTopic(AddTopicViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await _context.User.FirstOrDefaultAsync(t => t.Id.Equals(_userManager.GetUserId(User)));

                Topic topic = new Topic()
                {
                    Title = model.Topic,
                    Rate  = 0,
                    Date  = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                    User  = user
                };

                Comment comment = new Comment()
                {
                    Content = model.Comment,
                    Date    = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                    User    = user,
                    Topic   = topic
                };

                user.TopicsCount++;
                user.CommentsCount++;

                _context.Update(user);
                await _context.AddAsync(topic);

                await _context.AddAsync(comment);

                await _context.SaveChangesAsync();

                return(RedirectToAction("ShowTopic", new { id = topic.Id }));
            }
            return(View(model));
        }
コード例 #4
0
        public async Task <IActionResult> Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _context.Users.FirstOrDefaultAsync(t => t.Email.Equals(model.Email));

                if (user is null)
                {
                    return(RedirectToAction("MainPage", "Forum"));
                }

                var result = await _signInManager.PasswordSignInAsync(user.UserName, model.Password, false, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    user.LastLoginDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm");

                    _context.Update(user);
                    await _context.SaveChangesAsync();
                }
            }
            return(RedirectToAction("MainPage", "Forum"));
        }