예제 #1
0
        public UserInfoOutput GetDetailBySessionId([FromBody] UserInfoInput input)
        {
            UserInfoOutput output = new UserInfoOutput();

            if (input == null)
            {
                output.Result = "INPUT_IS_NULL";
            }
            else
            {
                AppLoginSession session = _db.AppLoginSessions.Where(e => e.Id.Equals(input.SessionId) && e.Status == 1).FirstOrDefault();

                if (session == null)
                {
                    output.Result = "SESSION_NOT_EXIST";
                }
                else
                {
                    if (session.Key.Equals(input.SessionKey))
                    {
                        User                     user      = session.User;
                        List <Order>             orders    = user.ListOrders.Where(e => e.Deleted == false).OrderByDescending(e => e.DateCreated).ToList();
                        List <OrderPreviousItem> newOrders = new List <OrderPreviousItem>();
                        NumberFormatInfo         nfi       = new CultureInfo("ms-MY", false).NumberFormat;
                        nfi.CurrencyDecimalDigits = 2;

                        foreach (Order item in orders)
                        {
                            OrderPreviousItem newItem = new OrderPreviousItem()
                            {
                                OrderId   = item.Id,
                                OrderDate = item.DateCreated.ToString(),
                                Price     = item.Amount.ToString("C", nfi)
                            };
                            newOrders.Add(newItem);
                        }

                        if (string.IsNullOrEmpty(user.Email))
                        {
                            output.IsMember = false;
                        }
                        else
                        {
                            output.IsMember = true;
                        }
                        output.Orders     = newOrders;
                        output.UserEmail  = user.Email;
                        output.UserName   = user.FName + user.LName;
                        output.DateJoined = user.DateCreated.ToString();
                        output.Result     = "OK";
                    }
                    else
                    {
                        output.Result = "CREDENTIAL_ERROR";
                    }
                }
            }

            return(output);
        }
예제 #2
0
        public SessionService(ApplicationDbContext db, string sessionId, string sessionKey)
        {
            _db        = db;
            SessionId  = sessionId;
            SessionKey = sessionKey;
            AppLoginSession session = _db.AppLoginSessions.Where(e => e.Id.Equals(SessionId) && e.Key.Equals(SessionKey) && e.Status == 1).FirstOrDefault();

            if (session == null)
            {
                IsValid = false;
            }
            else
            {
                IsValid = true;
                User    = _db._Users.Where(e => e.Id.Equals(session.User.Id)).FirstOrDefault();
            }
        }
예제 #3
0
        public CreateUserOutput CreateTempUser()
        {
            CreateUserOutput output  = new CreateUserOutput();
            User             newUser = new User();

            _db._Users.Add(newUser);
            AppLoginSession loginSession = new AppLoginSession(Guid.NewGuid().ToString(), Request);

            loginSession.User   = newUser;
            loginSession.Status = 1;
            _db.AppLoginSessions.Add(loginSession);
            _db.SaveChanges();

            output.UserId     = newUser.Id;
            output.SessionId  = loginSession.Id;
            output.SessionKey = loginSession.Key;

            return(output);
        }
예제 #4
0
        public LoginUserOutput Login([FromBody] LoginUserInput input)
        {
            LoginUserOutput output = new LoginUserOutput();

            if (string.IsNullOrEmpty(input.Email) || string.IsNullOrEmpty(input.Password))
            {
                output.Result = "FIELD_INCOMPLETE";
            }
            else
            {
                IdentityUser aspUser = _db._AspNetUsers.Where(e => e.UserName.ToLower().Equals(input.Email.ToLower())).FirstOrDefault();
                if (aspUser == null)
                {
                    output.Result = "USER_NOT_FOUND";
                }
                else
                {
                    if (_userManager.PasswordHasher.VerifyHashedPassword(aspUser, aspUser.PasswordHash, input.Password) == PasswordVerificationResult.Success)
                    {
                        AppLoginSession newSession = new AppLoginSession(Guid.NewGuid().ToString(), Request);
                        newSession.User   = _db._Users.Where(e => e.AspNetUser.Equals(aspUser)).FirstOrDefault();
                        newSession.Status = 1;
                        _db.AppLoginSessions.Add(newSession);
                        _db.SaveChanges();
                        output.SessionId = newSession.Id;
                        output.Key       = newSession.Key;
                        output.Result    = "OK";
                    }
                    else
                    {
                        output.Result = "PASSWORD_MISMATCH";
                    }
                }
            }
            return(output);
        }
예제 #5
0
        public GenerateCaptureOutput Index([FromBody] GenerateCaptureInput input)
        {
            GenerateCaptureOutput output = new GenerateCaptureOutput();

            if (input == null)
            {
                output.Result = "INPUT_IS_NULL";
            }
            else
            {
                if (input.Type == 1)
                {
                    MemberCapture mc = new MemberCapture()
                    {
                        Type = 1
                    };

                    _db.MemberCaptures.Add(mc);
                    _db.SaveChanges();

                    output.CaptureId   = mc.Id;
                    output.CaptureCode = mc.Code;
                    string[] urlFrag = Request.GetDisplayUrl().Split('/');
                    output.CaptureUrl = urlFrag[0] + "//" + urlFrag[2] + "/capture?" + "CaptureId=" + output.CaptureId + "&CaptureCode=" + output.CaptureCode;
                    output.CaptureQR  = urlFrag[0] + "//" + urlFrag[2] + "/qrcode/" + QRCodeService.GenerateQRCode(_hostingEnvironment, output.CaptureUrl);
                    output.Result     = "OK";
                }
                else if (input.Type == 2)
                {
                    AppLoginSession session = _db.AppLoginSessions.Where(e => e.Id.Equals(input.SessionId) && e.Status == 1).FirstOrDefault();

                    if (session == null)
                    {
                        output.Result = "SESSION_NOT_FOUND";
                    }
                    else
                    {
                        if (!session.Key.Equals(input.SessionKey))
                        {
                            output.Result = "WRONG_KEY";
                        }
                        else
                        {
                            MemberCapture mc = new MemberCapture()
                            {
                                AppLoginSession = session,
                                Type            = 2
                            };

                            _db.MemberCaptures.Add(mc);
                            _db.SaveChanges();

                            string[] urlFrag = Request.GetDisplayUrl().Split('/');

                            output.CaptureId   = mc.Id;
                            output.CaptureCode = mc.Code;
                            output.CaptureUrl  = urlFrag[0] + "//" + urlFrag[2] + "/capture?" + "CaptureId=" + output.CaptureId + "&CaptureCode=" + output.CaptureCode;
                            output.CaptureQR   = urlFrag[0] + "//" + urlFrag[2] + "/qrcode/" + QRCodeService.GenerateQRCode(_hostingEnvironment, output.CaptureUrl);
                            output.Result      = "OK";
                        }
                    }
                }
                else
                {
                    output.Result = "TYPE_NOT_EXIST";
                }
            }



            return(output);
        }
예제 #6
0
        public IActionResult Index(string CaptureId, string CaptureCode)
        {
            /*
             * ViewBag status
             *  1 - Capture detail not found (error)
             *  2 - QR Expired
             *  3 - Detail updated to existing session
             *  4 - New session added to client
             *  5 - Detail captured to server
             *  6 - No capture registered (no initial capture)
             */
            if (string.IsNullOrEmpty(CaptureId) || string.IsNullOrEmpty(CaptureCode))
            {
                ViewBag.Status = 1;
            }
            else
            {
                MemberCapture capture = _db.MemberCaptures.Where(e => e.Id.Equals(CaptureId) && e.Code.Equals(CaptureCode) && e.Deleted == false).FirstOrDefault();
                if (capture == null)
                {
                    ViewBag.Status = 2;
                }
                else
                {
                    if (capture.Status == 1)
                    {
                        AspUserService aspUser = new AspUserService(_db, this);

                        if (capture.Type == 1)
                        {
                            if (aspUser.IsValid)
                            {
                                MemberCapture newCapture = _db.MemberCaptures.Where(e => e.Id.Equals(CaptureId) && e.Code.Equals(CaptureCode) && e.Deleted == false && e.Status == 1).FirstOrDefault();

                                if (newCapture == null)
                                {
                                    ViewBag.Status = 2;
                                }
                                else
                                {
                                    AppLoginSession newSession = new AppLoginSession(Guid.NewGuid().ToString(), Request)
                                    {
                                        User   = aspUser.User,
                                        Status = 1
                                    };

                                    newCapture.AppLoginSession = newSession;
                                    newCapture.Status          = 2;
                                    _db.AppLoginSessions.Add(newSession);
                                    _db.SaveChanges();

                                    ViewBag.Status = 5;
                                }
                            }
                            else
                            {
                                if (Request.Cookies["CaptureId"] != null && Request.Cookies["CaptureCode"] != null)
                                {
                                    MemberCapture existingCapture = _db.MemberCaptures.Where(e => e.Id.Equals(Request.Cookies["CaptureId"].ToString()) && e.Code.Equals(Request.Cookies["CaptureCode"].ToString()) && e.Deleted == false && e.Status == 2).FirstOrDefault();

                                    if (existingCapture == null)
                                    {
                                        ViewBag.Status = 2;
                                    }
                                    else
                                    {
                                        MemberCapture newCapture = _db.MemberCaptures.Where(e => e.Id.Equals(CaptureId) && e.Code.Equals(CaptureCode) && e.Deleted == false && e.Status == 1).FirstOrDefault();
                                        newCapture.AppLoginSession = existingCapture.AppLoginSession;
                                        newCapture.Status          = 2;

                                        _db.SaveChanges();
                                        ViewBag.Status = 5;
                                    }
                                }
                                else
                                {
                                    ViewBag.Status = 6;
                                }
                            }
                        }
                        else if (capture.Type == 2)
                        {
                            if (aspUser.IsValid)
                            {
                                List <Order> orders = capture.AppLoginSession.User.ListOrders.ToList();
                                foreach (Order item in orders)
                                {
                                    item.User = aspUser.User;
                                }
                                capture.Status = 2;
                                _db.SaveChanges();

                                ViewBag.Status = 3;
                            }
                            else if (Request.Cookies["CaptureId"] != null)
                            {
                                MemberCapture existingCapture = _db.MemberCaptures.Where(e => e.Id.Equals(Request.Cookies["CaptureId"].ToString())).FirstOrDefault();
                                if (existingCapture == null)
                                {
                                    ViewBag.Status = 1;
                                }
                                else
                                {
                                    if (existingCapture.Code.Equals(Request.Cookies["CaptureCode"].ToString()) && existingCapture.Status == 2)
                                    {
                                        List <Order> orders = capture.AppLoginSession.User.ListOrders.ToList();
                                        foreach (Order item in orders)
                                        {
                                            item.User = existingCapture.AppLoginSession.User;
                                        }
                                        capture.Status = 2;

                                        Response.Cookies.Append("CaptureId", existingCapture.Id,
                                                                new Microsoft.AspNetCore.Http.CookieOptions()
                                        {
                                            Expires  = DateTime.UtcNow.AddYears(5),
                                            HttpOnly = true,
                                            Secure   = true,
                                            SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax
                                        });;
                                        Response.Cookies.Append("CaptureCode", existingCapture.Code,
                                                                new Microsoft.AspNetCore.Http.CookieOptions()
                                        {
                                            Expires  = DateTime.UtcNow.AddYears(5),
                                            HttpOnly = true,
                                            Secure   = true,
                                            SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax
                                        });

                                        _db.SaveChanges();
                                        ViewBag.Status = 3;
                                    }
                                    else
                                    {
                                        ViewBag.Status = 1;
                                    }
                                }
                                ViewBag.Status = 3;
                            }
                            else
                            {
                                capture.Status = 2;
                                _db.SaveChanges();
                                Response.Cookies.Append("CaptureId", capture.Id,
                                                        new Microsoft.AspNetCore.Http.CookieOptions()
                                {
                                    Expires  = DateTime.UtcNow.AddYears(5),
                                    HttpOnly = true,
                                    Secure   = true,
                                    SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax
                                });;
                                Response.Cookies.Append("CaptureCode", capture.Code,
                                                        new Microsoft.AspNetCore.Http.CookieOptions()
                                {
                                    Expires  = DateTime.UtcNow.AddYears(5),
                                    HttpOnly = true,
                                    Secure   = true,
                                    SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax
                                });
                                ViewBag.Status = 4;
                            }
                        }
                    }
                    else
                    {
                        ViewBag.Status = 2;
                    }
                }
            }
            return(View());
        }