示例#1
0
        public async Task <IActionResult> Login(Models.Login model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            StringContent content = new StringContent(JsonSerializer.Serialize(model, typeof(Models.Login)), Encoding.UTF8, "application/json");

            HttpResponseMessage httpResponse = await apiClient.PostAsync("/account/login", content);

            var result = await httpResponse.Content.ReadAsStringAsync();

            if (httpResponse.IsSuccessStatusCode)
            {
                JwtSecurityToken jwtSecurityToken = new JwtSecurityTokenHandler().ReadJwtToken(result);

                ClaimsIdentity identity = new ClaimsIdentity(jwtSecurityToken.Claims, CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaim(new Claim("token", result));

                ClaimsPrincipal principal = new ClaimsPrincipal(identity);
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties()
                {
                });

                Dtos.Pessoa pessoa = ObterLoginPessoa(model.email);

                if (pessoa != null)
                {
                    HttpContext.Session.SetString("username", pessoa.nomesocial ?? pessoa.nome);
                    if (pessoa.imagem != null)
                    {
                        HttpContext.Session.SetString("userpicture", pessoa.imagem);
                    }
                }
                else
                {
                    HttpContext.Session.SetString("username", model.email);
                }
                if (!string.IsNullOrEmpty(model.returnUrl))
                {
                    return(Redirect(model.returnUrl));
                }
                return(RedirectToAction("dashboard", "home"));
            }
            else
            {
                if (httpResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    ModelState.AddModelError("password", "Login/senha incorreto");
                    return(View(model));
                }
            }

            return(View());
        }
示例#2
0
        public IActionResult Embaixador(Dtos.Pessoa model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            StringContent content = new StringContent(JsonSerializer.Serialize(model, typeof(Dtos.Pessoa)), Encoding.UTF8, "application/json");

            HttpResponseMessage httpResponse = apiClient.PostAsync("/pessoa/create", content).Result;

            if (httpResponse.IsSuccessStatusCode)
            {
                return(Ok());
            }

            return(BadRequest());
        }