using System; using System.Collections.Generic; using System.Net.Http; using System.Net; using Xunit; using Microsoft.AspNet.WebUtilities; namespace E2ETests { public partial class SmokeTests { private void LoginWithMicrosoftAccount() { _httpClientHandler = new HttpClientHandler() { AllowAutoRedirect = false }; _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_applicationBaseUrl) }; var response = _httpClient.GetAsync("Account/Login").Result; ThrowIfResponseStatusNotOk(response); var responseContent = response.Content.ReadAsStringAsync().Result; Console.WriteLine("Signing in with Microsoft account"); var formParameters = new List> { new KeyValuePair("provider", "Microsoft"), new KeyValuePair("returnUrl", "/"), new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), }; var content = new FormUrlEncodedContent(formParameters.ToArray()); response = _httpClient.PostAsync("Account/ExternalLogin", content).Result; Assert.Equal("https://login.live.com/oauth20_authorize.srf", response.Headers.Location.AbsoluteUri.Replace(response.Headers.Location.Query, string.Empty)); var queryItems = QueryHelpers.ParseQuery(response.Headers.Location.Query); Assert.Equal("code", queryItems["response_type"]); Assert.Equal("[ClientId]", queryItems["client_id"]); Assert.Equal(_applicationBaseUrl + "signin-microsoft", queryItems["redirect_uri"]); Assert.Equal("wl.basic wl.signin", queryItems["scope"]); Assert.Equal("ValidStateData", queryItems["state"]); Assert.Equal("custom", queryItems["custom_redirect_uri"]); //Check for the correlation cookie Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Correlation.Microsoft")); //This is just to generate a correlation cookie. Previous step would generate this cookie, but we have reset the handler now. _httpClientHandler = new HttpClientHandler() { AllowAutoRedirect = true }; _httpClient = new HttpClient(_httpClientHandler) { BaseAddress = new Uri(_applicationBaseUrl) }; response = _httpClient.GetAsync("Account/Login").Result; responseContent = response.Content.ReadAsStringAsync().Result; formParameters = new List> { new KeyValuePair("provider", "Microsoft"), new KeyValuePair("returnUrl", "/"), new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLogin")), }; content = new FormUrlEncodedContent(formParameters.ToArray()); response = _httpClient.PostAsync("Account/ExternalLogin", content).Result; //Post a message to the MicrosoftAccount middleware response = _httpClient.GetAsync("signin-microsoft?code=ValidCode&state=ValidStateData").Result; ThrowIfResponseStatusNotOk(response); responseContent = response.Content.ReadAsStringAsync().Result; //Correlation cookie not getting cleared after successful signin? if (!Helpers.RunningOnMono) { Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Correlation.Microsoft")); } Assert.Equal(_applicationBaseUrl + "Account/ExternalLoginCallback?ReturnUrl=%2F", response.RequestMessage.RequestUri.AbsoluteUri); formParameters = new List> { new KeyValuePair("Email", "microsoft@test.com"), new KeyValuePair("__RequestVerificationToken", HtmlDOMHelper.RetrieveAntiForgeryToken(responseContent, "/Account/ExternalLoginConfirmation?ReturnUrl=%2F")), }; content = new FormUrlEncodedContent(formParameters.ToArray()); response = _httpClient.PostAsync("Account/ExternalLoginConfirmation", content).Result; ThrowIfResponseStatusNotOk(response); responseContent = response.Content.ReadAsStringAsync().Result; Assert.Contains(string.Format("Hello {0}!", "microsoft@test.com"), responseContent, StringComparison.OrdinalIgnoreCase); Assert.Contains("Log off", responseContent, StringComparison.OrdinalIgnoreCase); //Verify cookie sent Assert.NotNull(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.Application")); Assert.Null(_httpClientHandler.CookieContainer.GetCookies(new Uri(_applicationBaseUrl)).GetCookieWithName(".AspNet.Microsoft.AspNet.Identity.ExternalLogin")); Console.WriteLine("Successfully signed in with user '{0}'", "microsoft@test.com"); Console.WriteLine("Verifying if the middleware notifications were fired"); //Check for a non existing item response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", "123")).Result; //This action requires admin permissions. If notifications are fired this permission is granted Console.WriteLine(response.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Console.WriteLine("Middleware notifications were fired successfully"); } } }