Example #1
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services">
        /// The services.
        /// </param>
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services
            .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
            .AddBasicAuthentication(WebHostBuilderHelper.ConfigureOptions());

            services.AddMvc();
        }
Example #2
0
        public async Task UnauthorizedBasicTestWithOptions()
        {
            using var server        = new TestServer(WebHostBuilderHelper.CreateBuilder(o => { }));
            using HttpClient client = server.CreateClient();

            // Act
            HttpResponseMessage response = await client.GetAsync("api/test");

            // Assert
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "StatusCode != Unauthorized");
        }
Example #3
0
        public async Task AuthorizedCredentialsTestWithDi()
        {
            using var server        = new TestServer(WebHostBuilderHelper.CreateBuilder());
            using HttpClient client = server.CreateClient();

            // Arrange
            client.DefaultRequestHeaders.Add(HeaderNames.Authorization, AuthorizationHeaderHelper.GetBasic());

            // Act
            HttpResponseMessage response = await client.GetAsync("api/test");

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "StatusCode != OK");
        }
Example #4
0
        public async Task UnauthorizedBasicRealmTestWithOptions()
        {
            using var server        = new TestServer(WebHostBuilderHelper.CreateBuilder(o => { }));
            using HttpClient client = server.CreateClient();

            // Act
            HttpResponseMessage response = await client.GetAsync("api/test");

            // Assert
            AuthenticationHeaderValue wwwAuth = response.Headers.WwwAuthenticate.Single();
            NameValueHeaderValue      nvh     = NameValueHeaderValue.Parse(wwwAuth.Parameter);

            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "StatusCode != Unauthorized");
            Assert.AreEqual("Basic", wwwAuth.Scheme, "Scheme != Basic");
            Assert.AreEqual("realm", nvh.Name, "!realm");
            Assert.AreEqual("\"Basic Realm\"", nvh.Value, "!basic realm");
        }
Example #5
0
        public async Task UnauthorizedMyRealmTestAjaxRequestSuppressed()
        {
            using var server = new TestServer(WebHostBuilderHelper.CreateBuilder(o =>
            {
                o.Realm = "My realm";
                o.AjaxRequestOptions.SuppressWwwAuthenticateHeader = true;
            }));
            using HttpClient client = server.CreateClient();
            client.DefaultRequestHeaders.Add(Basic.BasicAuthenticationDefaults.AjaxRequestHeaderName, Basic.BasicAuthenticationDefaults.AjaxRequestHeaderValue);

            // Act
            HttpResponseMessage response = await client.GetAsync("api/test");

            // Assert
            AuthenticationHeaderValue wwwAuth = response.Headers.WwwAuthenticate.SingleOrDefault();

            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "StatusCode != Unauthorized");
            Assert.IsNull(wwwAuth, "No header should be sent back on ajax request");
        }
Example #6
0
        public async Task UnauthorizedWrongHeaderTest()
        {
            using var server        = new TestServer(WebHostBuilderHelper.CreateStartupBuilder());
            using HttpClient client = server.CreateClient();

            // Arrange
            client.DefaultRequestHeaders.Add(HeaderNames.Authorization, "Basic");

            // Act
            HttpResponseMessage response = await client.GetAsync("api/test");

            // Assert
            AuthenticationHeaderValue wwwAuth = response.Headers.WwwAuthenticate.Single();
            NameValueHeaderValue      nvh     = NameValueHeaderValue.Parse(wwwAuth.Parameter);

            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode, "StatusCode != Unauthorized");
            Assert.AreEqual("Basic", wwwAuth.Scheme, "Scheme != Basic");
            Assert.AreEqual("realm", nvh.Name, "!realm");
            Assert.AreEqual("\"My realm\"", nvh.Value, "!My realm");
        }