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 responseBody = await getResponse.Content.ReadAsStringAsync(); var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody, "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 antiforgery token could not be decrypted.", exception.ExceptionMessage); }
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 responseBody = await getResponse.Content.ReadAsStringAsync(); var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody, "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()); }
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 responseBody = 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(responseBody, "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()); }
public async Task Antiforgery_HeaderNotSet_SendsBadRequest() { // Arrange var getResponse = await Client.GetAsync("http://localhost/Antiforgery/Login"); var responseBody = await getResponse.Content.ReadAsStringAsync(); var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken( responseBody, "Antiforgery/Login"); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Antiforgery/Login"); 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.BadRequest, response.StatusCode); }
public async Task MissingCookieToken_Throws() { // Arrange // do a get response. var getResponse = await Client.GetAsync("http://localhost/Account/Login"); var responseBody = await getResponse.Content.ReadAsStringAsync(); var formToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody, "Account/Login"); var cookieTokenKey = AntiforgeryTestHelper.RetrieveAntiforgeryCookie(getResponse).Key; var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Account/Login"); 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 required antiforgery cookie \"" + cookieTokenKey + "\" is not present.", exception.ExceptionMessage); }
public async Task HtmlGenerationWebSite_GeneratesExpectedResults(string action, string antiforgeryPath) { // This uses FileVersionProvider which uses Uri.TryCreate - https://github.com/aspnet/External/issues/21 if (TestPlatformHelper.IsMono && (action == "Link" || action == "Script" || action == "Image")) { return; } // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); var outputFile = "compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home." + action + ".html"; var expectedContent = await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile : false); // Act // The host is not important as everything runs in memory and tests are isolated from each other. var response = await client.GetAsync("http://localhost/HtmlGeneration_Home/" + action); var responseContent = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); responseContent = responseContent.Trim(); if (antiforgeryPath == null) { #if GENERATE_BASELINES ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); #else // Mono issue - https://github.com/aspnet/External/issues/19 Assert.Equal( PlatformNormalizer.NormalizeContent(expectedContent.Trim()), responseContent, ignoreLineEndingDifferences: true); #endif } else { var forgeryToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, antiforgeryPath); #if GENERATE_BASELINES // Reverse usual substitution and insert a format item into the new file content. responseContent = responseContent.Replace(forgeryToken, "{0}"); ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); #else expectedContent = string.Format(expectedContent, forgeryToken); // Mono issue - https://github.com/aspnet/External/issues/19 Assert.Equal( PlatformNormalizer.NormalizeContent(expectedContent.Trim()), responseContent, ignoreLineEndingDifferences: true); #endif } }
public async Task ValidationTagHelpers_GeneratesExpectedSpansAndDivs() { // Arrange var server = TestHelper.CreateServer(_app, SiteName, _configureServices); var client = server.CreateClient(); var outputFile = "compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Customer.Index.html"; var expectedContent = await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile : false); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Customer/HtmlGeneration_Customer"); var nameValueCollection = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("Number", string.Empty), new KeyValuePair <string, string>("Name", string.Empty), new KeyValuePair <string, string>("Email", string.Empty), new KeyValuePair <string, string>("PhoneNumber", string.Empty), new KeyValuePair <string, string>("Password", string.Empty) }; request.Content = new FormUrlEncodedContent(nameValueCollection); // Act var response = await client.SendAsync(request); var responseContent = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); responseContent = responseContent.Trim(); var forgeryToken = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseContent, "Customer/HtmlGeneration_Customer"); #if GENERATE_BASELINES // Reverse usual substitution and insert a format item into the new file content. responseContent = responseContent.Replace(forgeryToken, "{0}"); ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent); #else expectedContent = string.Format(expectedContent, forgeryToken); // Mono issue - https://github.com/aspnet/External/issues/19 Assert.Equal( PlatformNormalizer.NormalizeContent(expectedContent.Trim()), responseContent, ignoreLineEndingDifferences: true); #endif }
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 antiforgery cookie and token values. var getResponse1 = await client.GetAsync("http://localhost/Account/Login"); var responseBody1 = await getResponse1.Content.ReadAsStringAsync(); var formToken1 = AntiforgeryTestHelper.RetrieveAntiforgeryToken(responseBody1, "Account/Login"); var getResponse2 = await client.GetAsync("http://localhost/Account/Login"); var responseBody2 = 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 antiforgery cookie token and form field token do not match.", exception.ExceptionMessage); }