public void LogInProcess()
        {
            string securedActionUrl = "/home/SecretAction";

            appHost.Start(browsingSession =>
            {
                // First try to request a secured page without being logged in
                RequestResult initialRequestResult = browsingSession.Get(securedActionUrl);
                string loginRedirectUrl            = initialRequestResult.Response.RedirectLocation;
                Assert.IsTrue(loginRedirectUrl.StartsWith("/Account/LogOn"), "Didn't redirect to logon page");

                // Now follow redirection to logon page
                string loginFormResponseText    = browsingSession.Get(loginRedirectUrl).ResponseText;
                string suppliedAntiForgeryToken = MvcUtils.ExtractAntiForgeryToken(loginFormResponseText);

                // Now post the login form, including the verification token
                RequestResult loginResult = browsingSession.Post(loginRedirectUrl, new
                {
                    UserName = "******",
                    Password = "******",
                    __RequestVerificationToken = suppliedAntiForgeryToken
                });
                string afterLoginRedirectUrl = loginResult.Response.RedirectLocation;
                Assert.AreEqual(securedActionUrl, afterLoginRedirectUrl, "Didn't redirect back to SecretAction");

                // Check that we can now follow the redirection back to the protected action, and are let in
                RequestResult afterLoginResult = browsingSession.Get(securedActionUrl);
                Assert.AreEqual("Hello, you're logged in as steve", afterLoginResult.ResponseText);
            });
        }
Example #2
0
        public void TestFixtureSetUp()
        {
            //If you MVC project is not in the root of your solution directory then include the path
            //e.g. AppHost.Simulate("Website\MyMvcApplication")
            appHost = AppHost.Simulate("MyMvcApplication");
            appHost.StartBrowsingSession();

            //login
            appHost.Start(browsingSession =>
            {
                //follow redirection to logon page
                var loginRedirectUrl            = "/Account/LogOn";
                string loginFormResponseText    = browsingSession.Get(loginRedirectUrl).ResponseText;
                string suppliedAntiForgeryToken = MvcUtils.ExtractAntiForgeryToken(loginFormResponseText);

                // Now post the login form, including the verification token
                browsingSession.Post(loginRedirectUrl, new
                {
                    UserName = "******",
                    Password = "******",
                    __RequestVerificationToken = suppliedAntiForgeryToken
                });
            });
        }