Example #1
0
        public async Task <ActionResult> Login(VMLogin login, string formValue)
        {
            PetshopGateway.Petshop.LoginResult result = new PetshopGateway.Petshop.LoginResult();

            result = await _api.ExecuteLogin(login);

            if (!result.Succeeded)
            {
                ModelState.AddModelError("loginError", result.ErrorMessage);
                login.Email    = "";
                login.Password = "";
                return(View(login));
            }

            //_api.Token = result.Token;

            var userObject = JsonConvert.SerializeObject(result.LoginProperties.User);

            List <Claim> claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, result.LoginProperties.User.Login),
                new Claim("Token", result.LoginProperties.Token),
                new Claim("UserId", result.LoginProperties.User.ClientUserId.ToString()),
            };
            ClaimsIdentity  claimsIdentity  = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
            ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);

            HttpContext.Session.SetString("Token", result.LoginProperties.Token);
            HttpContext.Session.SetString("User", userObject);
            HttpContext.Session.SetString("UserId", result.LoginProperties.User.ClientUserId.ToString());

            //SetStringToSession("Token", result.LoginProperties.Token);
            //SetStringToSession("User", userObject);
            //SetStringToSession("UserId", result.LoginProperties.User.ClientUserId.ToString());


            //Pra pegar => HttpContext.Session.GetString("Token")
            //var token = HttpContext.Session.GetString("Token");
            //Para armazena obj =>
            //var str = JsonConvert.SerializeObject(obj);
            //HttpContext.Session.SetString("OBJECT", str);

            return(RedirectToAction("Index"));
        }
Example #2
0
        public async Task <ActionResult> CreateAccount(VMLogin login, string formValue)
        {
            PetshopGateway.Petshop.LoginResult result = new PetshopGateway.Petshop.LoginResult();
            result = await _api.CreateUser(login);

            if (!result.Succeeded)
            {
                ModelState.AddModelError("signUpError", result.ErrorMessage);
                login.Email    = "";
                login.Password = "";
                return(View(login));
            }

            var userObject = JsonConvert.SerializeObject(result.LoginProperties.User);

            //SetStringToSession("Token", result.LoginProperties.Token);
            //SetStringToSession("User", userObject);

            HttpContext.Session.SetString("Token", result.LoginProperties.Token);
            HttpContext.Session.SetString("User", userObject);

            return(RedirectToAction("Index"));
        }