diff --git a/AADIntegration.sln b/AADIntegration.sln index 9289706e3b..2ed6ef8876 100644 --- a/AADIntegration.sln +++ b/AADIntegration.sln @@ -19,6 +19,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authen EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.AzureAD.UI.Test", "test\Microsoft.AspNetCore.Authentication.AzureAD.UI.Test\Microsoft.AspNetCore.Authentication.AzureAD.UI.Test.csproj", "{3D0CF896-3A9D-4A8F-A343-A2E1A131C861}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests", "test\Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests\Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests.csproj", "{1967296B-614B-43E5-B1BA-A601579961D5}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WebSites", "WebSites", "{2AEF59C3-7ADA-404D-B965-26B700FBD03F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AzureAD.WebSite", "test\WebSites\AzureAD.WebSite\AzureAD.WebSite.csproj", "{4DB3C5B2-9FBF-42BC-80B2-2190EB1AE316}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +51,14 @@ Global {3D0CF896-3A9D-4A8F-A343-A2E1A131C861}.Debug|Any CPU.Build.0 = Debug|Any CPU {3D0CF896-3A9D-4A8F-A343-A2E1A131C861}.Release|Any CPU.ActiveCfg = Release|Any CPU {3D0CF896-3A9D-4A8F-A343-A2E1A131C861}.Release|Any CPU.Build.0 = Release|Any CPU + {1967296B-614B-43E5-B1BA-A601579961D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1967296B-614B-43E5-B1BA-A601579961D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1967296B-614B-43E5-B1BA-A601579961D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1967296B-614B-43E5-B1BA-A601579961D5}.Release|Any CPU.Build.0 = Release|Any CPU + {4DB3C5B2-9FBF-42BC-80B2-2190EB1AE316}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4DB3C5B2-9FBF-42BC-80B2-2190EB1AE316}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4DB3C5B2-9FBF-42BC-80B2-2190EB1AE316}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4DB3C5B2-9FBF-42BC-80B2-2190EB1AE316}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -55,6 +69,9 @@ Global {454089F9-ED16-4A11-9C52-2BA74DCF5D35} = {57F46508-E53D-4F6B-B77C-2EFE95925AEF} {1762840C-A14A-4498-9883-CC671956F0F2} = {75A812B0-D98C-45F3-B2A9-357BBDF7331A} {3D0CF896-3A9D-4A8F-A343-A2E1A131C861} = {57F46508-E53D-4F6B-B77C-2EFE95925AEF} + {1967296B-614B-43E5-B1BA-A601579961D5} = {57F46508-E53D-4F6B-B77C-2EFE95925AEF} + {2AEF59C3-7ADA-404D-B965-26B700FBD03F} = {57F46508-E53D-4F6B-B77C-2EFE95925AEF} + {4DB3C5B2-9FBF-42BC-80B2-2190EB1AE316} = {2AEF59C3-7ADA-404D-B965-26B700FBD03F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C6DBF56C-E862-46EA-A4E0-993D2950D78D} diff --git a/build/dependencies.props b/build/dependencies.props index 54858ba2e9..41ee9964de 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -4,24 +4,36 @@ 2.1.0-preview3-17001 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 - 2.1.0-preview2-30464 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 + 2.1.0-preview2-30484 2.0.0 - 2.1.0-preview2-26314-02 + 2.1.0-preview2-26325-03 15.6.1 4.7.49 0.8.0 2.3.1 2.4.0-beta.1.build3945 + + diff --git a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/Areas/AzureAD/Pages/_ViewStart.cshtml b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/Areas/AzureAD/Pages/_ViewStart.cshtml index a5f10045db..8b7ed03b87 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/Areas/AzureAD/Pages/_ViewStart.cshtml +++ b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/Areas/AzureAD/Pages/_ViewStart.cshtml @@ -1,3 +1,13 @@ -@{ - Layout = "_Layout"; -} +@using Microsoft.AspNetCore.Hosting +@using Microsoft.AspNetCore.Mvc.ViewEngines + +@inject IHostingEnvironment Environment +@inject ICompositeViewEngine Engine + +@{ + var result = Engine.FindView(ViewContext, "_Layout", isMainPage: false); + if (result.Success) + { + Layout = "_Layout"; + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADAuthenticationBuilderExtensions.cs b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADAuthenticationBuilderExtensions.cs index 9abdc3c687..9afbcdee27 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADAuthenticationBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADAuthenticationBuilderExtensions.cs @@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Authentication builder.Services.TryAddSingleton, JwtBearerOptionsConfiguration>(); builder.Services.Configure(scheme, configureOptions); - builder.AddJwtBearer(); + builder.AddJwtBearer(jwtBearerScheme, o => { }); return builder; } diff --git a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADOptions.cs b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADOptions.cs index a1b9fa3f5c..89a8a84beb 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADOptions.cs +++ b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/AzureADOptions.cs @@ -34,6 +34,11 @@ namespace Microsoft.AspNetCore.Authentication.AzureAD.UI /// public string ClientId { get; set; } + /// + /// Gets or sets the client secret. + /// + public string ClientSecret { get; set; } + /// /// Gets or sets the tenant Id. /// diff --git a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/OpenIdConnectOptionsConfiguration.cs b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/OpenIdConnectOptionsConfiguration.cs index f2332a2231..57ca1329a4 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/OpenIdConnectOptionsConfiguration.cs +++ b/src/Microsoft.AspNetCore.Authentication.AzureAD.UI/OpenIdConnectOptionsConfiguration.cs @@ -28,6 +28,7 @@ namespace Microsoft.AspNetCore.Authentication.AzureAD.UI } options.ClientId = azureADOptions.ClientId; + options.ClientSecret = azureADOptions.ClientSecret; options.Authority = new Uri(new Uri(azureADOptions.Instance), azureADOptions.TenantId).ToString(); options.CallbackPath = azureADOptions.CallbackPath ?? options.CallbackPath; options.SignedOutCallbackPath = azureADOptions.SignedOutCallbackPath ?? options.SignedOutCallbackPath; diff --git a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/Areas/AzureADB2C/Pages/_ViewStart.cshtml b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/Areas/AzureADB2C/Pages/_ViewStart.cshtml index a5f10045db..8b7ed03b87 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/Areas/AzureADB2C/Pages/_ViewStart.cshtml +++ b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/Areas/AzureADB2C/Pages/_ViewStart.cshtml @@ -1,3 +1,13 @@ -@{ - Layout = "_Layout"; -} +@using Microsoft.AspNetCore.Hosting +@using Microsoft.AspNetCore.Mvc.ViewEngines + +@inject IHostingEnvironment Environment +@inject ICompositeViewEngine Engine + +@{ + var result = Engine.FindView(ViewContext, "_Layout", isMainPage: false); + if (result.Success) + { + Layout = "_Layout"; + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2CAuthenticationBuilderExtensions.cs b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2CAuthenticationBuilderExtensions.cs index b133bc5c6f..23d567a5e4 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2CAuthenticationBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2CAuthenticationBuilderExtensions.cs @@ -52,7 +52,6 @@ namespace Microsoft.AspNetCore.Authentication string jwtBearerScheme, Action configureOptions) { - builder.AddPolicyScheme(scheme, displayName: null, configureOptions: o => { o.ForwardDefault = jwtBearerScheme; @@ -65,7 +64,7 @@ namespace Microsoft.AspNetCore.Authentication builder.Services.TryAddSingleton, JwtBearerOptionsConfiguration>(); builder.Services.Configure(scheme, configureOptions); - builder.AddJwtBearer(); + builder.AddJwtBearer(jwtBearerScheme, o => { }); return builder; } diff --git a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2COptions.cs b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2COptions.cs index 6209bebb91..64b5ed0fd2 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2COptions.cs +++ b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/AzureAdB2COptions.cs @@ -35,6 +35,11 @@ namespace Microsoft.AspNetCore.Authentication.AzureADB2C.UI /// public string ClientId { get; set; } + /// + /// Gets or sets the client secret. + /// + public string ClientSecret { get; set; } + /// /// Gets or sets the Azure Active Directory B2C instance. /// diff --git a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/OpenIdConnectOptionsConfiguration.cs b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/OpenIdConnectOptionsConfiguration.cs index 439f613985..2150e26b17 100644 --- a/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/OpenIdConnectOptionsConfiguration.cs +++ b/src/Microsoft.AspNetCore.Authentication.AzureADB2C.UI/OpenIdConnectOptionsConfiguration.cs @@ -30,6 +30,7 @@ namespace Microsoft.AspNetCore.Authentication.AzureADB2C.UI } options.ClientId = azureADB2COptions.ClientId; + options.ClientSecret = azureADB2COptions.ClientSecret; options.Authority = BuildAuthority(azureADB2COptions); options.CallbackPath = azureADB2COptions.CallbackPath ?? options.CallbackPath; options.SignedOutCallbackPath = azureADB2COptions.SignedOutCallbackPath ?? options.SignedOutCallbackPath; diff --git a/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/ApiAuthenticationTests.cs b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/ApiAuthenticationTests.cs new file mode 100644 index 0000000000..1bd9718ac3 --- /dev/null +++ b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/ApiAuthenticationTests.cs @@ -0,0 +1,91 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.using Microsoft.AspNetCore.Authorization; + +using System.Net; +using System.Threading.Tasks; +using AzureAD.WebSite; +using Microsoft.AspNetCore.Authentication.AzureAD.UI; +using Microsoft.AspNetCore.Authentication.AzureADB2C.UI; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; +using Xunit; + +namespace Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests +{ + public class ApiAuthenticationTests : IClassFixture> + { + public ApiAuthenticationTests(WebApplicationFactory fixture) + { + Factory = fixture; + } + + public WebApplicationFactory Factory { get; } + + [Fact] + public async Task BearerAzureAD_Challenges_UnauthorizedRequests() + { + // Arrange + var client = Factory.WithWebHostBuilder(builder => builder.ConfigureTestServices( + services => + { + services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme) + .AddAzureADBearer(o => + { + o.Instance = "https://login.microsoftonline.com/"; + o.Domain = "test.onmicrosoft.com"; + o.ClientId = "ClientId"; + o.TenantId = "TenantId"; + }); + + services.Configure(AzureADDefaults.JwtBearerAuthenticationScheme, o => + { + o.Configuration = new OpenIdConnectConfiguration() + { + Issuer = "https://www.example.com", + }; + }); + })).CreateDefaultClient(); + + // Act + var response = await client.GetAsync("/api/get"); + + // Assert + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + } + + [Fact] + public async Task BearerAzureADB2C_Challenges_UnauthorizedRequests() + { + // Arrange + var client = Factory.WithWebHostBuilder(builder => builder.ConfigureTestServices( + services => + { + services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme) + .AddAzureADB2CBearer(o => + { + o.Instance = "https://login.microsoftonline.com/"; + o.Domain = "test.onmicrosoft.com"; + o.ClientId = "ClientId"; + o.SignUpSignInPolicyId = "B2c_1_SiSu"; + }); + + services.Configure(AzureADB2CDefaults.JwtBearerAuthenticationScheme, o => + { + o.Configuration = new OpenIdConnectConfiguration() + { + Issuer = "https://www.example.com", + }; + }); + })).CreateDefaultClient(); + + // Act + var response = await client.GetAsync("/api/get"); + + // Assert + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests.csproj b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests.csproj new file mode 100644 index 0000000000..f0cb193135 --- /dev/null +++ b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests.csproj @@ -0,0 +1,21 @@ + + + + $(StandardTestTfms) + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/WebAuthenticationTests.cs b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/WebAuthenticationTests.cs new file mode 100644 index 0000000000..dc8e5f8271 --- /dev/null +++ b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/WebAuthenticationTests.cs @@ -0,0 +1,162 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.using Microsoft.AspNetCore.Authorization; + +using System.Net; +using System.Threading.Tasks; +using AzureAD.WebSite; +using Microsoft.AspNetCore.Authentication.AzureAD.UI; +using Microsoft.AspNetCore.Authentication.AzureADB2C.UI; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc.Authorization; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Protocols.OpenIdConnect; +using Xunit; + +namespace Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests +{ + public class WebAuthenticationTests : IClassFixture> + { + public WebAuthenticationTests(WebApplicationFactory fixture) + { + Factory = fixture; + } + + public WebApplicationFactory Factory { get; } + + public static TheoryData NotAddedEndpoints => + new TheoryData() + { + "/AzureAD/Account/AccessDenied", + "/AzureAD/Account/Error", + "/AzureAD/Account/SignedOut", + "/AzureAD/Account/SignIn", + "/AzureAD/Account/SignOut", + "/AzureADB2C/Account/AccessDenied", + "/AzureADB2C/Account/Error", + "/AzureADB2C/Account/SignedOut", + "/AzureADB2C/Account/SignIn", + "/AzureADB2C/Account/ResetPassword", + "/AzureADB2C/Account/EditProfile", + "/AzureADB2C/Account/SignOut", + }; + + [Theory] + [MemberData(nameof(NotAddedEndpoints))] + public async Task Endpoints_NotAvailable_When_Authentication_NotAdded(string endpoint) + { + // Act & Assert + var response = await Factory.CreateDefaultClient().GetAsync(endpoint); + + // Assert + Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); + } + + public static TheoryData AddedEndpointsStatusCodesAD => + new TheoryData() + { + { "/AzureAD/Account/AccessDenied", HttpStatusCode.OK }, + { "/AzureAD/Account/Error", HttpStatusCode.OK }, + { "/AzureAD/Account/SignedOut", HttpStatusCode.OK }, + { "/AzureAD/Account/SignIn", HttpStatusCode.Redirect }, + { "/AzureAD/Account/SignOut", HttpStatusCode.Redirect }, + }; + + [Theory] + [MemberData(nameof(AddedEndpointsStatusCodesAD))] + public async Task ADEndpoints_AreAvailable_When_Authentication_IsAdded(string endpoint, HttpStatusCode expectedStatusCode) + { + // Act & Assert + var client = Factory.WithWebHostBuilder(builder => builder.ConfigureTestServices( + services => + { + services + .AddAuthentication(AzureADDefaults.AuthenticationScheme) + .AddAzureAD(o => + { + o.Instance = "https://login.microsoftonline.com/"; + o.Domain = "test.onmicrosoft.com"; + o.ClientId = "ClientId"; + o.TenantId = "TenantId"; + }); + + services.Configure(AzureADDefaults.OpenIdScheme, o => + { + o.Configuration = new OpenIdConnectConfiguration() + { + Issuer = "https://www.example.com", + TokenEndpoint = "https://www.example.com/token", + AuthorizationEndpoint = "https://www.example.com/authorize", + EndSessionEndpoint = "https://www.example.com/logout" + }; + }); + + services.AddMvc(o => o.Filters.Add( + new AuthorizeFilter(new AuthorizationPolicyBuilder(new[] { AzureADDefaults.AuthenticationScheme }) + .RequireAuthenticatedUser().Build()))); + })).CreateDefaultClient(); + + var response = await client.GetAsync(endpoint); + + // Assert + Assert.Equal(expectedStatusCode, response.StatusCode); + } + + public static TheoryData AddedEndpointsStatusCodesADB2C => + new TheoryData() + { + { "/AzureADB2C/Account/AccessDenied", HttpStatusCode.OK }, + { "/AzureADB2C/Account/Error", HttpStatusCode.OK }, + { "/AzureADB2C/Account/SignedOut", HttpStatusCode.OK }, + { "/AzureADB2C/Account/SignIn", HttpStatusCode.Redirect }, + { "/AzureADB2C/Account/ResetPassword", HttpStatusCode.Redirect }, + { "/AzureADB2C/Account/EditProfile", HttpStatusCode.Redirect }, + { "/AzureADB2C/Account/SignOut", HttpStatusCode.Redirect } + }; + + [Theory] + [MemberData(nameof(AddedEndpointsStatusCodesADB2C))] + public async Task ADB2CEndpoints_AreAvailable_When_Authentication_IsAdded(string endpoint, HttpStatusCode expectedStatusCode) + { + // Act & Assert + var client = Factory.WithWebHostBuilder(builder => builder.ConfigureTestServices( + services => + { + services + .AddAuthentication(AzureADB2CDefaults.AuthenticationScheme) + .AddAzureADB2C(o => + { + o.Instance = "https://login.microsoftonline.com/tfp/"; + o.ClientId = "ClientId"; + o.CallbackPath = "/signin-oidc"; + o.Domain = "test.onmicrosoft.com"; + o.SignUpSignInPolicyId = "B2C_1_SiUpIn"; + o.ResetPasswordPolicyId = "B2C_1_SSPR"; + o.EditProfilePolicyId = "B2C_1_SiPe"; + }); + + services.Configure(AzureADB2CDefaults.OpenIdScheme, o => + { + o.Configuration = new OpenIdConnectConfiguration() + { + Issuer = "https://www.example.com", + TokenEndpoint = "https://www.example.com/token", + AuthorizationEndpoint = "https://www.example.com/authorize", + EndSessionEndpoint = "https://www.example.com/logout" + }; + }); + + services.AddMvc(o => o.Filters.Add( + new AuthorizeFilter(new AuthorizationPolicyBuilder(new[] { AzureADB2CDefaults.AuthenticationScheme }) + .RequireAuthenticatedUser().Build()))); + })).CreateDefaultClient(); + + var response = await client.GetAsync(endpoint); + + // Assert + Assert.Equal(expectedStatusCode, response.StatusCode); + } + } +} diff --git a/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/xunit.runner.json b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/xunit.runner.json new file mode 100644 index 0000000000..42db7ef95e --- /dev/null +++ b/test/Microsoft.AspNetCore.Authentication.AzureAD.FunctionalTests/xunit.runner.json @@ -0,0 +1,3 @@ +{ + "shadowCopy": false +} diff --git a/test/Microsoft.AspNetCore.Authentication.AzureAD.UI.Test/AzureADAuthenticationBuilderExtensionsTests.cs b/test/Microsoft.AspNetCore.Authentication.AzureAD.UI.Test/AzureADAuthenticationBuilderExtensionsTests.cs index 50250c8f35..a5bf4d61cb 100644 --- a/test/Microsoft.AspNetCore.Authentication.AzureAD.UI.Test/AzureADAuthenticationBuilderExtensionsTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.AzureAD.UI.Test/AzureADAuthenticationBuilderExtensionsTests.cs @@ -47,6 +47,7 @@ namespace Microsoft.AspNetCore.Authentication { o.Instance = "https://login.microsoftonline.com"; o.ClientId = "ClientId"; + o.ClientSecret = "ClientSecret"; o.CallbackPath = "/signin-oidc"; o.Domain = "domain.onmicrosoft.com"; o.TenantId = "Common"; @@ -61,6 +62,7 @@ namespace Microsoft.AspNetCore.Authentication Assert.Equal(AzureADDefaults.CookieScheme, azureADOptions.CookieSchemeName); Assert.Equal("https://login.microsoftonline.com", azureADOptions.Instance); Assert.Equal("ClientId", azureADOptions.ClientId); + Assert.Equal("ClientSecret", azureADOptions.ClientSecret); Assert.Equal("/signin-oidc", azureADOptions.CallbackPath); Assert.Equal("domain.onmicrosoft.com", azureADOptions.Domain); diff --git a/test/Microsoft.AspNetCore.Authentication.AzureADB2C.UI.Test/AzureAdB2CAuthenticationBuilderExtensionsTests.cs b/test/Microsoft.AspNetCore.Authentication.AzureADB2C.UI.Test/AzureAdB2CAuthenticationBuilderExtensionsTests.cs index d545047fe2..d6e7ba36d7 100644 --- a/test/Microsoft.AspNetCore.Authentication.AzureADB2C.UI.Test/AzureAdB2CAuthenticationBuilderExtensionsTests.cs +++ b/test/Microsoft.AspNetCore.Authentication.AzureADB2C.UI.Test/AzureAdB2CAuthenticationBuilderExtensionsTests.cs @@ -47,6 +47,7 @@ namespace Microsoft.AspNetCore.Authentication { o.Instance = "https://login.microsoftonline.com/tfp"; o.ClientId = "ClientId"; + o.ClientSecret = "ClientSecret"; o.CallbackPath = "/signin-oidc"; o.Domain = "domain.onmicrosoft.com"; o.SignUpSignInPolicyId = "B2C_1_SiUpIn"; @@ -63,6 +64,7 @@ namespace Microsoft.AspNetCore.Authentication Assert.Equal(AzureADB2CDefaults.CookieScheme, azureADB2COptions.CookieSchemeName); Assert.Equal("https://login.microsoftonline.com/tfp", azureADB2COptions.Instance); Assert.Equal("ClientId", azureADB2COptions.ClientId); + Assert.Equal("ClientSecret", azureADB2COptions.ClientSecret); Assert.Equal("/signin-oidc", azureADB2COptions.CallbackPath); Assert.Equal("domain.onmicrosoft.com", azureADB2COptions.Domain); Assert.Equal("B2C_1_SiUpIn", azureADB2COptions.SignUpSignInPolicyId); diff --git a/test/WebSites/AzureAD.WebSite/AzureAD.WebSite.csproj b/test/WebSites/AzureAD.WebSite/AzureAD.WebSite.csproj new file mode 100644 index 0000000000..b42cbf7947 --- /dev/null +++ b/test/WebSites/AzureAD.WebSite/AzureAD.WebSite.csproj @@ -0,0 +1,28 @@ + + + + $(StandardTestWebsiteTfms) + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/WebSites/AzureAD.WebSite/Controllers/TestController.cs b/test/WebSites/AzureAD.WebSite/Controllers/TestController.cs new file mode 100644 index 0000000000..5340cd1a16 --- /dev/null +++ b/test/WebSites/AzureAD.WebSite/Controllers/TestController.cs @@ -0,0 +1,15 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.using Microsoft.AspNetCore.Authorization; + +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace AzureAD.WebSite.Controllers +{ + public class TestController : Controller + { + [Authorize] + [HttpGet("/api/get")] + public IActionResult Get() => Ok(); + } +} diff --git a/test/WebSites/AzureAD.WebSite/Program.cs b/test/WebSites/AzureAD.WebSite/Program.cs new file mode 100644 index 0000000000..01046c73a0 --- /dev/null +++ b/test/WebSites/AzureAD.WebSite/Program.cs @@ -0,0 +1,77 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Reflection; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; + +namespace AzureAD.WebSite +{ + public class Program + { + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) + { + var builder = new WebHostBuilder() + .UseKestrel((builderContext, options) => + { + options.Configure(builderContext.Configuration.GetSection("Kestrel")); + }) + .UseContentRoot(Directory.GetCurrentDirectory()) + .ConfigureAppConfiguration((hostingContext, config) => + { + var env = hostingContext.HostingEnvironment; + + config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); + + if (env.IsDevelopment()) + { + var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName)); + if (appAssembly != null) + { + config.AddUserSecrets(appAssembly, optional: true); + } + } + + config.AddEnvironmentVariables(); + + if (args != null) + { + config.AddCommandLine(args); + } + }) + .ConfigureLogging((hostingContext, logging) => + { + logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); + logging.AddConsole(); + logging.AddDebug(); + }) + .UseIISIntegration() + .UseDefaultServiceProvider((context, options) => + { + options.ValidateScopes = context.HostingEnvironment.IsDevelopment(); + }); + + if (args != null) + { + builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build()); + } + + builder.UseStartup(); + + return builder; + } + } +} diff --git a/test/WebSites/AzureAD.WebSite/Startup.cs b/test/WebSites/AzureAD.WebSite/Startup.cs new file mode 100644 index 0000000000..251b482fc6 --- /dev/null +++ b/test/WebSites/AzureAD.WebSite/Startup.cs @@ -0,0 +1,24 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; + +namespace AzureAD.WebSite +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + services.AddMvc(); + } + + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + app.UseAuthentication(); + + app.UseMvc(); + } + } +} diff --git a/test/WebSites/Directory.Build.props b/test/WebSites/Directory.Build.props new file mode 100644 index 0000000000..5d16f204a2 --- /dev/null +++ b/test/WebSites/Directory.Build.props @@ -0,0 +1,11 @@ + + + + + + netcoreapp2.1 + $(DeveloperBuildTestWebsiteTfms) + $(StandardTestWebsiteTfms);netcoreapp2.0 + $(StandardTestWebsiteTfms);net461 + + \ No newline at end of file