Exemplo n.º 1
0
        public async void LoginFailure()
        {
            var client = new FlashcardClient("https://*****:*****@yahoo.com", "Pa$$w0rd");

            user.Should().BeNull();
        }
Exemplo n.º 2
0
        public async void LoginSuccess()
        {
            var client = new FlashcardClient("https://*****:*****@yahoo.com", "Pa$$w0rd");

            user.Should().NotBeNull();
            user.DisplayName.Should().Be("Dilip Agheda");
            user.Token.Should().NotBeNull();
        }
        public async Task <IActionResult> Login(LoginViewModel loginViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(loginViewModel));
            }

            var applicationUser = await _flashcardClient.Login(loginViewModel.Email, loginViewModel.Password);

            if (applicationUser == null)
            {
                ModelState.AddModelError("loginError", "Login failed. User either not exist or invalid password.");
                return(View(loginViewModel));
            }

            var claims = new List <Claim>
            {
                new Claim("AccessToken", applicationUser.Token),
                new Claim(ClaimTypes.Name, applicationUser.DisplayName)
            };

            var claimsIdentity = new ClaimsIdentity(
                claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties
            {
                //AllowRefresh = <bool>,
                // Refreshing the authentication session should be allowed.

                //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                // The time at which the authentication ticket expires. A
                // value set here overrides the ExpireTimeSpan option of
                // CookieAuthenticationOptions set with AddCookie.

                //IsPersistent = true,
                // Whether the authentication session is persisted across
                // multiple requests. When used with cookies, controls
                // whether the cookie's lifetime is absolute (matching the
                // lifetime of the authentication ticket) or session-based.

                //IssuedUtc = <DateTimeOffset>,
                // The time at which the authentication ticket was issued.

                //RedirectUri = <string>
                // The full path or absolute URI to be used as an http
                // redirect response value.
            };

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties);

            if (Url.IsLocalUrl(loginViewModel.ReturnUrl))
            {
                return(Redirect(loginViewModel.ReturnUrl));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }