//[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" }));
            }
        }
        public async Task <IActionResult> Create([Bind("id,first_name,last_name,birth_date,profile_img,profile_background,gender,phone_number,email,password,reenter_password")] User user, IFormFile profile_img)
        {
            if (ModelState.IsValid)
            {
                if (user.password.Equals(user.reenter_password))
                {
                    var users      = data.getConnection().GetCollection <User_Authorization>("User");
                    var check_user = users.Find(x => x.authorization.username == user.email).SingleAsync();
                    try
                    {
                        User_Authorization au = check_user.Result;
                        return(BadRequest(new { Message = "Email is already existed" }));
                    }
                    catch (AggregateException ae)
                    {
                    }
                    // specify that we want to randomly generate a 20-byte salt
                    using (var deriveBytes = new Rfc2898DeriveBytes(user.password, 20))
                    {
                        byte[] salt = deriveBytes.Salt;
                        byte[] key  = deriveBytes.GetBytes(20); // derive a 20-byte key
                        Models.Authorization autho = new Models.Authorization {
                            username = user.email, salt = salt, key = key
                        };
                        user.created_date = DateTime.Now;
                        user.last_login   = DateTime.Now;
                        if (profile_img != null)
                        {
                            var path    = _env.WebRootPath;
                            var uploads = Path.Combine(path, "img");
                            user.profile_img = "/img/" + save_fileAsync(uploads, profile_img);
                        }
                        else
                        {
                            user.profile_img = "/img/unknown.jpg";
                        }
                        await users.InsertOneAsync(new User_Authorization { authorization = autho, user = user });

                        // save salt and key to database
                    }
                    return(Ok("Insert successfully"));
                }
                else
                {
                    return(BadRequest(new { Message = "Password and Re-enter password must match" }));
                }
            }
            else
            {
                return(BadRequest(new { Message = "Please fill all fields" }));
            }
        }
Exemplo n.º 3
0
        public async Task registrationAsync(User_Authorization user_auth)
        {
            NotificationManagement nm  = new NotificationManagement();
            UserStoryManagement    usm = new UserStoryManagement();
            var users = dataContext.getConnection().GetCollection <User_Authorization>("User");
            await users.InsertOneAsync(user_auth);

            await nm.insert_notificationAsync(new Notification_Record { _id = user_auth._id.ToString(), notifications = new List <Notification>() });

            await usm.insert_userStoryAsync(new Entities.UserStory {
                _id = user_auth._id.ToString(), user_stories = new List <Entities.Story>()
            });
        }
Exemplo n.º 4
0
        public Boolean checkEmailIsExist(string email)
        {
            var users      = dataContext.getConnection().GetCollection <User_Authorization>("User");
            var check_user = users.Find(x => x.authorization.username == email).SingleAsync();

            try
            {
                User_Authorization au = check_user.Result;
                return(true);
            }
            catch (AggregateException ae)
            {
                return(false);
            }
        }
Exemplo n.º 5
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));
            }
        }