Ejemplo n.º 1
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                DateTime dateSecret = DateTime.Now;
                string secret = null;

                TokenController tokenControler = new TokenController();
                LoginController loginControler = new LoginController();
                
                secret = tokenControler.getSecret("Gopag", dateSecret, model.Email);

                var token = tokenControler.getAccessToken(secret, model.Email, dateSecret.ToString("yyyyMMddHHmm"));

                if (token.@return)
                {

                    User appUser = new User();
                    appUser.password = model.Password;
                    appUser.system = 3;
                    appUser.ipAddress = UserHelper.GetIPAddress();
                    appUser.userAgent = UserHelper.GetUserAgent();
                    string jsonParams = JsonConvert.SerializeObject(appUser);

                    var login = loginControler.getLogin(((Token)token).accessToken, model.Email, jsonParams);

                    if (login.@return && ((Login)login).id > 0)
                    {
                        appUser.id = ((Login)login).id;
                        appUser.name = ((Login)login).name;
                        appUser.email = ((Login)login).email;
                        appUser.session = ((Login)login).session;
                        appUser.idMerchant = ((Login)login).idMerchant;

                        await SignInAsync(appUser, model.RememberMe, appUser, Response);
                        //return RedirectToAction("", "Sales");
                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("", "E-mail ou senha inválido.");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "E-mail ou senha inválido.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Ejemplo n.º 2
0
        private async Task SignInAsync(User user, bool isPersistent, User appUser, HttpResponseBase response)
        {
            Logoff(Session, Response);
            FormsAuthentication.SetAuthCookie(user.id.ToString(), isPersistent);

            var serializer = new JavaScriptSerializer();
            string userData = serializer.Serialize(appUser);

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                    appUser.email,
                    DateTime.Now,
                    DateTime.Now.AddDays(1),
                    true,
                    userData,
                    FormsAuthentication.FormsCookiePath);

            // Encrypt the ticket.
            string encTicket = FormsAuthentication.Encrypt(ticket);

            // Create the cookie.
            response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

        }