[Fixes #11] Add functional tests for the libraries

This commit is contained in:
Javier Calvarro Nelson 2018-03-25 12:23:27 -07:00
parent 804eeede11
commit ecafbe7f7a
21 changed files with 517 additions and 21 deletions

View File

@ -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}

View File

@ -4,24 +4,36 @@
</PropertyGroup>
<PropertyGroup Label="Package Versions">
<InternalAspNetCoreSdkPackageVersion>2.1.0-preview3-17001</InternalAspNetCoreSdkPackageVersion>
<MicrosoftAspNetCoreAuthenticationCookiesPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreAuthenticationCookiesPackageVersion>
<MicrosoftAspNetCoreAuthenticationJwtBearerPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreAuthenticationJwtBearerPackageVersion>
<MicrosoftAspNetCoreAuthenticationOpenIdConnectPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreAuthenticationOpenIdConnectPackageVersion>
<MicrosoftAspNetCoreHttpsPolicyPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreHttpsPolicyPackageVersion>
<MicrosoftAspNetCoreMvcPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreMvcPackageVersion>
<MicrosoftAspNetCoreServerIISIntegrationPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreServerIISIntegrationPackageVersion>
<MicrosoftAspNetCoreServerKestrelPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreServerKestrelPackageVersion>
<MicrosoftAspNetCoreStaticFilesPackageVersion>2.1.0-preview2-30464</MicrosoftAspNetCoreStaticFilesPackageVersion>
<MicrosoftExtensionsConfigurationJsonPackageVersion>2.1.0-preview2-30464</MicrosoftExtensionsConfigurationJsonPackageVersion>
<MicrosoftExtensionsLoggingConsolePackageVersion>2.1.0-preview2-30464</MicrosoftExtensionsLoggingConsolePackageVersion>
<MicrosoftExtensionsLoggingDebugPackageVersion>2.1.0-preview2-30464</MicrosoftExtensionsLoggingDebugPackageVersion>
<MicrosoftAspNetCoreAuthenticationCookiesPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreAuthenticationCookiesPackageVersion>
<MicrosoftAspNetCoreAuthenticationJwtBearerPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreAuthenticationJwtBearerPackageVersion>
<MicrosoftAspNetCoreAuthenticationOpenIdConnectPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreAuthenticationOpenIdConnectPackageVersion>
<MicrosoftAspNetCoreAuthorizationPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreAuthorizationPackageVersion>
<MicrosoftAspNetCoreDataProtectionExtensionsPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreDataProtectionExtensionsPackageVersion>
<MicrosoftAspNetCoreHostingPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreHostingPackageVersion>
<MicrosoftAspNetCoreHttpsPolicyPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreHttpsPolicyPackageVersion>
<MicrosoftAspNetCoreMvcPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreMvcPackageVersion>
<MicrosoftAspNetCoreMvcTestingPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreMvcTestingPackageVersion>
<MicrosoftAspNetCoreServerIISIntegrationPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreServerIISIntegrationPackageVersion>
<MicrosoftAspNetCoreServerKestrelHttpsPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreServerKestrelHttpsPackageVersion>
<MicrosoftAspNetCoreServerKestrelPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreServerKestrelPackageVersion>
<MicrosoftAspNetCoreStaticFilesPackageVersion>2.1.0-preview2-30484</MicrosoftAspNetCoreStaticFilesPackageVersion>
<MicrosoftExtensionsConfigurationCommandLinePackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsConfigurationCommandLinePackageVersion>
<MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>
<MicrosoftExtensionsConfigurationJsonPackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsConfigurationJsonPackageVersion>
<MicrosoftExtensionsConfigurationUserSecretsPackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsConfigurationUserSecretsPackageVersion>
<MicrosoftExtensionsLoggingConfigurationPackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsLoggingConfigurationPackageVersion>
<MicrosoftExtensionsLoggingConsolePackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsLoggingConsolePackageVersion>
<MicrosoftExtensionsLoggingDebugPackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsLoggingDebugPackageVersion>
<MicrosoftExtensionsLoggingTestingPackageVersion>2.1.0-preview2-30484</MicrosoftExtensionsLoggingTestingPackageVersion>
<MicrosoftNETCoreApp20PackageVersion>2.0.0</MicrosoftNETCoreApp20PackageVersion>
<MicrosoftNETCoreApp21PackageVersion>2.1.0-preview2-26314-02</MicrosoftNETCoreApp21PackageVersion>
<MicrosoftNETCoreApp21PackageVersion>2.1.0-preview2-26325-03</MicrosoftNETCoreApp21PackageVersion>
<MicrosoftNETTestSdkPackageVersion>15.6.1</MicrosoftNETTestSdkPackageVersion>
<MoqPackageVersion>4.7.49</MoqPackageVersion>
<XunitAnalyzersPackageVersion>0.8.0</XunitAnalyzersPackageVersion>
<XunitPackageVersion>2.3.1</XunitPackageVersion>
<XunitRunnerVisualStudioPackageVersion>2.4.0-beta.1.build3945</XunitRunnerVisualStudioPackageVersion>
</PropertyGroup>
<Import Project="$(DotNetPackageVersionPropsPath)" Condition=" '$(DotNetPackageVersionPropsPath)' != '' " />
</Project>

View File

@ -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";
}
}

View File

@ -63,7 +63,7 @@ namespace Microsoft.AspNetCore.Authentication
builder.Services.TryAddSingleton<IConfigureOptions<JwtBearerOptions>, JwtBearerOptionsConfiguration>();
builder.Services.Configure(scheme, configureOptions);
builder.AddJwtBearer();
builder.AddJwtBearer(jwtBearerScheme, o => { });
return builder;
}

View File

@ -34,6 +34,11 @@ namespace Microsoft.AspNetCore.Authentication.AzureAD.UI
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// Gets or sets the client secret.
/// </summary>
public string ClientSecret { get; set; }
/// <summary>
/// Gets or sets the tenant Id.
/// </summary>

View File

@ -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;

View File

@ -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";
}
}

View File

@ -52,7 +52,6 @@ namespace Microsoft.AspNetCore.Authentication
string jwtBearerScheme,
Action<AzureADB2COptions> configureOptions)
{
builder.AddPolicyScheme(scheme, displayName: null, configureOptions: o =>
{
o.ForwardDefault = jwtBearerScheme;
@ -65,7 +64,7 @@ namespace Microsoft.AspNetCore.Authentication
builder.Services.TryAddSingleton<IConfigureOptions<JwtBearerOptions>, JwtBearerOptionsConfiguration>();
builder.Services.Configure(scheme, configureOptions);
builder.AddJwtBearer();
builder.AddJwtBearer(jwtBearerScheme, o => { });
return builder;
}

View File

@ -35,6 +35,11 @@ namespace Microsoft.AspNetCore.Authentication.AzureADB2C.UI
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// Gets or sets the client secret.
/// </summary>
public string ClientSecret { get; set; }
/// <summary>
/// Gets or sets the Azure Active Directory B2C instance.
/// </summary>

View File

@ -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;

View File

@ -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<WebApplicationFactory<Startup>>
{
public ApiAuthenticationTests(WebApplicationFactory<Startup> fixture)
{
Factory = fixture;
}
public WebApplicationFactory<Startup> 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<JwtBearerOptions>(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<JwtBearerOptions>(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);
}
}
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
<PlatformTarget Condition=" '$(TargetFramework)' == 'netcoreapp2.1' "></PlatformTarget>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\WebSites\AzureAD.WebSite\AzureAD.WebSite.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(MicrosoftAspNetCoreMvcTestingPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="$(MicrosoftExtensionsLoggingTestingPackageVersion)" />
</ItemGroup>
</Project>

View File

@ -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<WebApplicationFactory<Startup>>
{
public WebAuthenticationTests(WebApplicationFactory<Startup> fixture)
{
Factory = fixture;
}
public WebApplicationFactory<Startup> Factory { get; }
public static TheoryData<string> NotAddedEndpoints =>
new TheoryData<string>()
{
"/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<string, HttpStatusCode> AddedEndpointsStatusCodesAD =>
new TheoryData<string, HttpStatusCode>()
{
{ "/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<OpenIdConnectOptions>(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<string, HttpStatusCode> AddedEndpointsStatusCodesADB2C =>
new TheoryData<string, HttpStatusCode>()
{
{ "/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<OpenIdConnectOptions>(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);
}
}
}

View File

@ -0,0 +1,3 @@
{
"shadowCopy": false
}

View File

@ -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);

View File

@ -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);

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>$(StandardTestWebsiteTfms)</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="$(MicrosoftAspNetCoreAuthorizationPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="$(MicrosoftAspNetCoreDataProtectionExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="$(MicrosoftAspNetCoreHostingPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(MicrosoftAspNetCoreMvcPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="$(MicrosoftAspNetCoreServerIISIntegrationPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(MicrosoftAspNetCoreServerKestrelPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="$(MicrosoftAspNetCoreServerKestrelHttpsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="$(MicrosoftExtensionsConfigurationUserSecretsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="$(MicrosoftExtensionsConfigurationCommandLinePackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="$(MicrosoftExtensionsLoggingConfigurationPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(MicrosoftExtensionsLoggingConsolePackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="$(MicrosoftExtensionsLoggingDebugPackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.Authentication.AzureAD.UI\Microsoft.AspNetCore.Authentication.AzureAD.UI.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AspNetCore.Authentication.AzureADB2C.UI\Microsoft.AspNetCore.Authentication.AzureADB2C.UI.csproj" />
</ItemGroup>
</Project>

View File

@ -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();
}
}

View File

@ -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<Startup>();
return builder;
}
}
}

View File

@ -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();
}
}
}

View File

@ -0,0 +1,11 @@
<Project>
<!-- Skip the parent directory so test websites do not get the test package references. -->
<Import Project="..\..\Directory.Build.props" />
<PropertyGroup>
<DeveloperBuildTestWebsiteTfms>netcoreapp2.1</DeveloperBuildTestWebsiteTfms>
<StandardTestWebsiteTfms>$(DeveloperBuildTestWebsiteTfms)</StandardTestWebsiteTfms>
<StandardTestWebsiteTfms Condition=" '$(DeveloperBuild)' != 'true' ">$(StandardTestWebsiteTfms);netcoreapp2.0</StandardTestWebsiteTfms>
<StandardTestWebsiteTfms Condition=" '$(DeveloperBuild)' != 'true' AND '$(OS)' == 'Windows_NT' ">$(StandardTestWebsiteTfms);net461</StandardTestWebsiteTfms>
</PropertyGroup>
</Project>