示例#1
0
        public void no_credentials_throws_unauthorized()
        {
            var client = GetClient();
            var request = new RequiresAuthenticationRequest {RequestData = "test"};
            // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
            var error = Assert.Throws<WebServiceException>(() => client.Send<RequiresAuthenticationResponse>(request));

            Assert.AreEqual((int) HttpStatusCode.Unauthorized, error.StatusCode);
            Assert.AreEqual("Unauthorized", error.StatusDescription);
            Assert.AreEqual("Unauthorized", error.ErrorCode);
        }
示例#2
0
        public void html_clients_receive_secured_url_including_query_string_within_login_page_redirect_query_string()
        {
            var client = (ServiceClientBase) GetHtmlClient();
            client.AllowAutoRedirect = false;
            string lastResponseLocationHeader = null;
            client.ResponseFilter = response => { lastResponseLocationHeader = response.Headers["Location"]; };

            var request = new RequiresAuthenticationRequest {RequestData = "test"};
            // Perform a GET so that the Name DTO field is encoded as query string.
            client.Get(request);

            var locationUri = new Uri(lastResponseLocationHeader);
            var locationUriQueryString = HttpUtility.ParseQueryString(locationUri.Query);
            var redirectQueryItem = locationUriQueryString["redirect"];
            var redirectUri = new Uri(redirectQueryItem);

            // Should contain the url attempted to access before the redirect to the login page,
            // including the 'Name=test' query string.
            var redirectUriQueryString = HttpUtility.ParseQueryString(redirectUri.Query);
            Assert.That(redirectUriQueryString.AllKeys, Contains.Item("requestData"));
            Assert.AreEqual("test", redirectUriQueryString["requestData"]);
        }
示例#3
0
        public void html_clients_receive_redirect_to_login_page_when_accessing_unauthenticated()
        {
            var client = (ServiceClientBase) GetHtmlClient();
            client.AllowAutoRedirect = false;
            string lastResponseLocationHeader = null;
            
            client.ResponseFilter = response => { lastResponseLocationHeader = response.Headers["Location"]; };

            var request = new RequiresAuthenticationRequest {RequestData = "test"};
            // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
            client.Send<RequiresAuthenticationResponse>(request);

            var locationUri = new Uri(lastResponseLocationHeader);
            var loginPath = "/".CombineWith(VirtualDirectory).CombineWith(SystemConstants.LoginUrl);
            Assert.That(locationUri.AbsolutePath, Is.EqualTo(loginPath).IgnoreCase);
        }
示例#4
0
        public void html_clients_receive_secured_url_attempt_in_login_page_redirect_query_string()
        {
            var client = (ServiceClientBase) GetHtmlClient();
            client.AllowAutoRedirect = false;
            string lastResponseLocationHeader = null;
            client.ResponseFilter = response => { lastResponseLocationHeader = response.Headers["Location"]; };

            var request = new RequiresAuthenticationRequest {RequestData = "test"};
            // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
            client.Send<RequiresAuthenticationResponse>(request);

            var locationUri = new Uri(lastResponseLocationHeader);
            var queryString = HttpUtility.ParseQueryString(locationUri.Query);
            var redirectQueryString = queryString["redirect"];
            var redirectUri = new Uri(redirectQueryString);

            // Should contain the url attempted to access before the redirect to the login page.
            var securedPath = "/".CombineWith(VirtualDirectory).CombineWith("requiresauthenticationrequest");
            Assert.That(redirectUri.AbsolutePath, Is.EqualTo(securedPath).IgnoreCase);
            // The url should also obey the WebHostUrl setting for the domain.
            var redirectSchemeAndHost = redirectUri.Scheme + "://" + redirectUri.Authority;
            var webHostUri = new Uri(WebHostUrl);
            var webHostSchemeAndHost = webHostUri.Scheme + "://" + webHostUri.Authority;
            Assert.That(redirectSchemeAndHost, Is.EqualTo(webHostSchemeAndHost).IgnoreCase);
        }
示例#5
0
        public async Task does_work_with_CredentailsAuth_Async()
        {
            var client = GetClient();

            var request = new RequiresAuthenticationRequest {RequestData = "test"};
            var authResponse = await client.SendAsync<AuthenticateResponse>(
                new Authenticate
                {
                    provider = CredentialsAuthProvider.Name,
                    UserName = "******",
                    Password = "******",
                    RememberMe = true,
                });

            authResponse.PrintDump();

            var response = await client.SendAsync<RequiresAuthenticationResponse>(request);

            Assert.That(response.Result, Is.EqualTo(request.RequestData));
        }
示例#6
0
        public void does_work_with_CredentailsAuth_Multiple_Times()
        {
            var client = GetClient();

            // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
            var authResponse = client.Send<AuthenticateResponse>(new Authenticate
            {
                provider = CredentialsAuthProvider.Name,
                UserName = "******",
                Password = "******",
                RememberMe = true,
            });

            Console.WriteLine(authResponse.Dump());

            for (int i = 0; i < 500; i++)
            {
                var request = new RequiresAuthenticationRequest {RequestData = "test"};
                // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
                var response = client.Send<RequiresAuthenticationResponse>(request);
                Assert.That(response.Result, Is.EqualTo(request.RequestData));
                Console.WriteLine("loop : {0}", i);
            }
        }
示例#7
0
        public void does_work_with_CredentailsAuth()
        {
            var client = GetClient();

            var authResponse = client.Send(new Authenticate
            {
                provider = CredentialsAuthProvider.Name,
                UserName = "******",
                Password = "******",
                RememberMe = true,
            });

            authResponse.PrintDump();

            var request = new RequiresAuthenticationRequest {RequestData = "test"};
            // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
            var response = client.Send<RequiresAuthenticationResponse>(request);
            Assert.That(response.Result, Is.EqualTo(request.RequestData));
        }
示例#8
0
        public void does_always_send_BasicAuth()
        {
            var client = (ServiceClientBase) GetClientWithUserPassword();
            client.AlwaysSendBasicAuthHeader = true;
            client.RequestFilter = req =>
            {
                bool hasAuthentication = false;
                foreach (var key in req.Headers.Keys)
                {
                    if (key.ToString() == "Authorization")
                        hasAuthentication = true;
                }
                Assert.IsTrue(hasAuthentication);
            };

            var request = new RequiresAuthenticationRequest {RequestData = "test"};
            // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
            var response = client.Send<RequiresAuthenticationResponse>(request);
            Assert.That(response.Result, Is.EqualTo(request.RequestData));
        }
示例#9
0
 public void does_work_with_BasicAuth()
 {
     var client = GetClientWithUserPassword();
     var request = new RequiresAuthenticationRequest {RequestData = "test"};
     // ReSharper disable once RedundantTypeArgumentsOfMethod  (Parameter type added for clarity only)
     var response = client.Send<RequiresAuthenticationResponse>(request);
     Assert.That(response.Result, Is.EqualTo(request.RequestData));
 }
 public object Post(RequiresAuthenticationRequest request)
 {
     return new RequiresAuthenticationResponse { Result = request.RequestData };
 }
 public object Get(RequiresAuthenticationRequest request)
 {
     throw new ArgumentException("unicorn nuggets");
 }