예제 #1
0
        public async Task ValidCredentialsAndNoRegisteredAuthenticationServiceReturns401()
        {
            var client = TestBed.GetClientWithBuilder(builder =>
            {
                builder.AddApiKeyHeaderAuthentication(options => options.UseRegisteredAuthenticationHandler = true);
            });

            client.UseApiKey("testapi");
            var response = await client.GetAsync("/");

            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
            Assert.Equal(string.Empty, await response.Content.ReadAsStringAsync());
        }
예제 #2
0
        public async Task InvalidCredentialsAndCustomAuthenticationServiceReturns401()
        {
            const string key = "badapi";

            var client = TestBed.GetClientWithBuilder(builder =>
            {
                builder.AddApiKeyHeaderAuthentication(options => options.UseRegisteredAuthenticationHandler = true);
                builder.Services.AddSingleton <IApiKeyCustomAuthenticator, TestApiKeyService>();
            });

            client.UseApiKey(key);
            var response = await client.GetAsync("/");

            Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
            Assert.Equal(string.Empty, await response.Content.ReadAsStringAsync());
        }
예제 #3
0
        public async Task ValidCredentialsAndCustomAuthenticationFullTicketProperlySetClaimsInContext()
        {
            const string key       = "goodkey";
            const string claimName = "John";

            var client = TestBed.GetClientWithBuilder(builder =>
            {
                builder.AddApiKeyHeaderAuthentication(options => options.UseRegisteredAuthenticationHandler = true);
                builder.Services.AddSingleton <IApiKeyCustomAuthenticationTicketHandler, CustomFullTicketHandler>();
            });

            client.UseApiKey(key);
            var response = await client.GetAsync(TestBed.FullUserPath);

            var content = await response.Content.ReadAsStringAsync();

            var user = JsonDocument.Parse(content);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(claimName, user.RootElement.GetProperty("Name").GetString());
        }
예제 #4
0
        public async Task ValidCredentialsAndCustomAuthenticationFullTicketProperlySetOtherClaimsInTicket()
        {
            const string key = "goodkey";

            var client = TestBed.GetClientWithBuilder(builder =>
            {
                builder.AddApiKeyHeaderAuthentication(options => options.UseRegisteredAuthenticationHandler = true);
                builder.Services.AddSingleton <IApiKeyCustomAuthenticationTicketHandler, CustomFullTicketHandler>();
            });

            client.UseApiKey(key);
            var response = await client.GetAsync(TestBed.FullTicketPrincipalClaimsPath);

            var content = await response.Content.ReadAsStringAsync();

            var claims = JsonDocument.Parse(content);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", claims.RootElement[0].GetProperty("Type").GetString());
            Assert.Equal(CustomFullTicketHandler.TestUserName, claims.RootElement[0].GetProperty("Value").GetString());
            Assert.Equal("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", claims.RootElement[1].GetProperty("Type").GetString());
            Assert.Equal(CustomFullTicketHandler.TestRole, claims.RootElement[1].GetProperty("Value").GetString());
        }