//[ValidateAntiForgeryToken]
        public async Task <IActionResult> Login([Bind("email,password,remember")] Login login)
        {
            Debug.WriteLine(login.remember);
            if (ModelState.IsValid)
            {
                try
                {
                    User_Authorization u = checkPassword.check_password(login.email, login.password);
                    var claims           = new List <Claim>
                    {
                        new Claim(ClaimTypes.PrimarySid, u._id.ToString()),
                        new Claim(ClaimTypes.Email, u.user.email),
                        new Claim(ClaimTypes.Name, u.user.first_name + " " + u.user.last_name),
                        new Claim(ClaimTypes.Actor, u.user.profile_img),
                        new Claim(ClaimTypes.Role, "User")
                    };

                    var             userIdentity = new ClaimsIdentity(claims, "User");
                    ClaimsPrincipal principal    = new ClaimsPrincipal(userIdentity);

                    if (login.remember)
                    {
                        //User_Session user_Session = new User_Session { user_name = u.user.first_name + " " + u.user.last_name, _id = u._id, profile_pic = u.user.profile_img };
                        //ISession session = HttpContext.Session;
                        //session.SetString("user", JsonConvert.SerializeObject(user_Session));
                        //var value = session.GetString("user");
                        //User_Session user = JsonConvert.DeserializeObject<User_Session>(value);
                        //Debug.WriteLine(user.user_name);
                        await HttpContext.SignInAsync(
                            scheme : "User",
                            principal : principal,
                            properties : new AuthenticationProperties
                        {
                            IsPersistent = true,
                            ExpiresUtc   = DateTime.UtcNow.AddDays(30)
                        });
                    }
                    else
                    {
                        await HttpContext.SignInAsync(scheme : "User", principal : principal);
                    }
                    var claimsPrincipal = new ClaimsPrincipal(userIdentity);
                    // Set current principal
                    Thread.CurrentPrincipal = claimsPrincipal;
                    return(Ok("/Posts"));
                }
                catch (AccountIsNotExistException accountnotexist)
                {
                    return(BadRequest(new { Message = accountnotexist.Message }));
                }
                catch (InvalidUserNameAndPasswordException invalidUserName)
                {
                    return(BadRequest(new { Message = invalidUserName.Message }));
                }
            }
            else
            {
                return(BadRequest(new { Message = "Please fill all fields" }));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Authorization([FromBody] Login login)
        {
            CheckPassword cp     = new CheckPassword();
            Helper        helper = new Helper();

            try
            {
                User_Authorization authorized_user = cp.check_password(login.UserName, login.PassWord);
                Owner userCookie = new Owner
                {
                    _id          = helper.EncodeTo64(authorized_user._id.ToString()),
                    user_name    = authorized_user.user.first_name + " " + authorized_user.user.last_name,
                    email        = authorized_user.user.email,
                    user_picture = authorized_user.user.profile_img
                };
                return(Ok(Json(new { data = userCookie })));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }