Exemplo n.º 1
0
        public async Task InvalidCookieToken_Throws()
        {
            // Arrange
            var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
            var client = server.CreateClient();

            var getResponse = await client.GetAsync("http://localhost/Account/Login");

            var resposneBody = await getResponse.Content.ReadAsStringAsync();

            var formToken = AntiForgeryTestHelper.RetrieveAntiForgeryToken(resposneBody, "Account/Login");

            var cookieToken = AntiForgeryTestHelper.RetrieveAntiForgeryCookie(getResponse);
            var request     = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/Login");

            request.Headers.Add("Cookie", cookieToken.Key + "=invalidCookie");

            var nameValueCollection = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("__RequestVerificationToken", formToken),
                new KeyValuePair <string, string>("UserName", "abra"),
                new KeyValuePair <string, string>("Password", "cadabra"),
            };

            request.Content = new FormUrlEncodedContent(nameValueCollection);

            // Act
            var response = await client.SendAsync(request);

            // Assert
            var exception = response.GetServerException();

            Assert.Equal("The anti-forgery token could not be decrypted.", exception.ExceptionMessage);
        }
Exemplo n.º 2
0
        public async Task MultipleFormPostWithingASingleView_AreAllowed()
        {
            // Arrange
            var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
            var client = server.CreateClient();

            // do a get response.
            var getResponse = await client.GetAsync("http://localhost/Account/Login");

            var resposneBody = await getResponse.Content.ReadAsStringAsync();

            // Get the AF token for the second login. If the cookies are generated twice(i.e are different),
            // this AF token will not work with the first cookie.
            var formToken   = AntiForgeryTestHelper.RetrieveAntiForgeryToken(resposneBody, "Account/UseFacebookLogin");
            var cookieToken = AntiForgeryTestHelper.RetrieveAntiForgeryCookie(getResponse);

            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/Login");

            request.Headers.Add("Cookie", cookieToken.Key + "=" + cookieToken.Value);
            var nameValueCollection = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("__RequestVerificationToken", formToken),
                new KeyValuePair <string, string>("UserName", "abra"),
                new KeyValuePair <string, string>("Password", "cadabra"),
            };

            request.Content = new FormUrlEncodedContent(nameValueCollection);

            // Act
            var response = await client.SendAsync(request);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal("OK", await response.Content.ReadAsStringAsync());
        }
Exemplo n.º 3
0
        public async Task SetCookieAndHeaderBeforeFlushAsync_PostToForm()
        {
            // Arrange
            var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
            var client = server.CreateClient();

            // do a get response.
            var getResponse = await client.GetAsync("http://localhost/Account/FlushAsyncLogin");

            var resposneBody = await getResponse.Content.ReadAsStringAsync();

            var formToken   = AntiForgeryTestHelper.RetrieveAntiForgeryToken(resposneBody, "Account/FlushAsyncLogin");
            var cookieToken = AntiForgeryTestHelper.RetrieveAntiForgeryCookie(getResponse);

            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/FlushAsyncLogin");

            request.Headers.Add("Cookie", cookieToken.Key + "=" + cookieToken.Value);
            var nameValueCollection = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("__RequestVerificationToken", formToken),
                new KeyValuePair <string, string>("UserName", "test"),
                new KeyValuePair <string, string>("Password", "password"),
            };

            request.Content = new FormUrlEncodedContent(nameValueCollection);

            // Act
            var response = await client.SendAsync(request);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal("OK", await response.Content.ReadAsStringAsync());
        }
Exemplo n.º 4
0
        public async Task MissingAFToken_Throws()
        {
            // Arrange
            var server      = TestServer.Create(_services, _app);
            var client      = server.CreateClient();
            var getResponse = await client.GetAsync("http://localhost/Account/Login");

            var resposneBody = await getResponse.Content.ReadAsStringAsync();

            var cookieToken = AntiForgeryTestHelper.RetrieveAntiForgeryCookie(getResponse);

            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/Login");

            request.Headers.Add("Cookie", "__RequestVerificationToken=" + cookieToken);
            var nameValueCollection = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("UserName", "abra"),
                new KeyValuePair <string, string>("Password", "cadabra"),
            };

            request.Content = new FormUrlEncodedContent(nameValueCollection);

            // Act
            var response = await client.SendAsync(request);

            // Assert
            var exception = response.GetServerException();

            Assert.Equal("The required anti-forgery form field \"__RequestVerificationToken\" is not present.",
                         exception.ExceptionMessage);
        }
Exemplo n.º 5
0
        public async Task InvalidFormToken_Throws()
        {
            // Arrange
            var server = TestServer.Create(_services, _app);
            var client = server.CreateClient();

            var getResponse = await client.GetAsync("http://localhost/Account/Login");

            var resposneBody = await getResponse.Content.ReadAsStringAsync();

            var cookieToken = AntiForgeryTestHelper.RetrieveAntiForgeryCookie(getResponse);
            var request     = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/Login");
            var formToken   = "adsad";

            request.Headers.Add("Cookie", "__RequestVerificationToken=" + cookieToken);
            var nameValueCollection = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("__RequestVerificationToken", formToken),
                new KeyValuePair <string, string>("UserName", "abra"),
                new KeyValuePair <string, string>("Password", "cadabra"),
            };

            request.Content = new FormUrlEncodedContent(nameValueCollection);

            // Act & Assert
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => client.SendAsync(request));

            Assert.Equal("The anti-forgery token could not be decrypted.", ex.Message);
        }
Exemplo n.º 6
0
        public async Task IncompatibleCookieToken_Throws()
        {
            // Arrange
            var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
            var client = server.CreateClient();

            // do a get response.
            // We do two requests to get two different sets of anti forgery cookie and token values.
            var getResponse1 = await client.GetAsync("http://localhost/Account/Login");

            var resposneBody1 = await getResponse1.Content.ReadAsStringAsync();

            var formToken1 = AntiForgeryTestHelper.RetrieveAntiForgeryToken(resposneBody1, "Account/Login");

            var getResponse2 = await client.GetAsync("http://localhost/Account/Login");

            var resposneBody2 = await getResponse2.Content.ReadAsStringAsync();

            var cookieToken2 = AntiForgeryTestHelper.RetrieveAntiForgeryCookie(getResponse2);

            var cookieToken = cookieToken2.Value;
            var request     = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/Login");

            request.Headers.Add("Cookie", string.Format("{0}={1}", cookieToken2.Key, cookieToken2.Value));
            var formToken           = formToken1;
            var nameValueCollection = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("__RequestVerificationToken", formToken),
                new KeyValuePair <string, string>("UserName", "abra"),
                new KeyValuePair <string, string>("Password", "cadabra"),
            };

            request.Content = new FormUrlEncodedContent(nameValueCollection);

            // Act
            var response = await client.SendAsync(request);

            // Assert
            var exception = response.GetServerException();

            Assert.Equal("The anti-forgery cookie token and form field token do not match.", exception.ExceptionMessage);
        }