Exemple #1
0
        public async Task Returns_username_and_password_login_form()
        {
            // Arrange
            var authenticationApiClient = new AuthenticationApiClient(new Uri(GetVariable("AUTH0_AUTHENTICATION_API_URL")));

            // Act
            var authenticationResponse = await authenticationApiClient.UsernamePasswordLoginAsync(new UsernamePasswordLoginRequest
            {
                ClientId    = GetVariable("AUTH0_CLIENT_ID"),
                Connection  = connection.Name,
                Scope       = "openid",
                Username    = user.Email,
                Password    = "******",
                RedirectUri = "http://www.blah.com/test"
            });

            // Assert
            authenticationResponse.Should().NotBeNull();
            authenticationResponse.HtmlForm.Should().NotBeNull();


            // Load the form, and submit it
            var configuration = Configuration.Default.WithDefaultLoader().WithCookies();
            var context       = BrowsingContext.New(configuration);
            await context.OpenAsync(request =>
            {
                request.Content(authenticationResponse.HtmlForm);
            });

            await context.Active.QuerySelector <IHtmlFormElement>("form").Submit();

            // Extract the URL and query from the postback
            var uri  = new Uri(context.Active.Url);
            var code = HttpUtility.ParseQueryString(uri.Query)["code"];

            // Assert that callback is made and code is passed back
            code.Should().NotBeNull();
        }
Exemple #2
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var redirectUri = Request.Url.Scheme + "://" + Request.Url.Authority + "/signin-auth0";

                    var response = await auth0.UsernamePasswordLoginAsync(new Auth0.AuthenticationApi.Models.UsernamePasswordLoginRequest()
                    {
                        Username     = model.UserName,
                        Tenant       = System.Configuration.ConfigurationManager.AppSettings["auth0:Domain"].Split('.')[0],
                        ClientId     = System.Configuration.ConfigurationManager.AppSettings["auth0:ClientId"],
                        Connection   = "Username-Password-Authentication", // You can change this to the name of your database.
                        ResponseType = "code",
                        RedirectUri  = redirectUri.ToString(),
                        Password     = model.Password,
                        Scope        = "openid email"
                    });

                    return(View("LoginSuccess", new LoginSuccessViewModel()
                    {
                        HtmlForm = response.HtmlForm
                    }));
                }
                catch (ApiException apiException)
                {
                    ModelState.AddModelError("", apiException.ApiError.Message);
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", "Internal server error.");
                }
            }

            return(View("Login", model));
        }
        public async Task Returns_username_and_password_login_form()
        {
            // Arrange
            var authenticationApiClient = new AuthenticationApiClient(new Uri(GetVariable("AUTH0_AUTHENTICATION_API_URL")));

            // Act
            var authenticationResponse = await authenticationApiClient.UsernamePasswordLoginAsync(new UsernamePasswordLoginRequest
            {
                ClientId = GetVariable("AUTH0_CLIENT_ID"),
                Connection = connection.Name,
                Scope = "openid",
                Username = user.Email,
                Password = "******",
                RedirectUri = "http://www.blah.com/test"
            });

            // Assert
            authenticationResponse.Should().NotBeNull();
            authenticationResponse.HtmlForm.Should().NotBeNull();


            // Load the form, and submit it
            var configuration = Configuration.Default.WithDefaultLoader().WithCookies();
            var context = BrowsingContext.New(configuration);
            await context.OpenAsync(request =>
            {
                request.Content(authenticationResponse.HtmlForm);
            });

            await context.Active.QuerySelector<IHtmlFormElement>("form").Submit();

            // Extract the URL and query from the postback
            var uri = new Uri(context.Active.Url);
            var code = HttpUtility.ParseQueryString(uri.Query)["code"];

            // Assert that callback is made and code is passed back
            code.Should().NotBeNull();
        }