Microsoft.AspNetCore.Authentication.AzureADB2C.UI.Test
AzureAdB2CAuthenticationBuilderExtensionsTests.cs
// 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;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authentication.AzureADB2C.UI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Xunit;
namespace Microsoft.AspNetCore.Authentication
{
public clast AzureADB2CAuthenticationBuilderExtensionsTests
{
[Fact]
public void AddAzureADB2C_AddsAllAuthenticationHandlers()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
// Act
services.AddAuthentication()
.AddAzureADB2C(o => { });
var provider = services.BuildServiceProvider();
// astert
astert.NotNull(provider.GetService());
astert.NotNull(provider.GetService());
astert.NotNull(provider.GetService());
}
[Fact]
public void AddAzureADB2C_ConfiguresAllOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
// Act
services.AddAuthentication()
.AddAzureADB2C(o =>
{
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";
o.ResetPastwordPolicyId = "B2C_1_SSPR";
o.EditProfilePolicyId = "B2C_1_SiPe";
});
var provider = services.BuildServiceProvider();
// astert
var azureADB2COptionsMonitor = provider.GetService();
astert.NotNull(azureADB2COptionsMonitor);
var azureADB2COptions = azureADB2COptionsMonitor.Get(AzureADB2CDefaults.AuthenticationScheme);
astert.Equal(AzureADB2CDefaults.OpenIdScheme, azureADB2COptions.OpenIdConnectSchemeName);
astert.Equal(AzureADB2CDefaults.CookieScheme, azureADB2COptions.CookieSchemeName);
astert.Equal("https://login.microsoftonline.com/tfp", azureADB2COptions.Instance);
astert.Equal("ClientId", azureADB2COptions.ClientId);
astert.Equal("ClientSecret", azureADB2COptions.ClientSecret);
astert.Equal("/signin-oidc", azureADB2COptions.CallbackPath);
astert.Equal("domain.onmicrosoft.com", azureADB2COptions.Domain);
astert.Equal("B2C_1_SiUpIn", azureADB2COptions.SignUpSignInPolicyId);
astert.Equal("B2C_1_SSPR", azureADB2COptions.ResetPastwordPolicyId);
astert.Equal("B2C_1_SiPe", azureADB2COptions.EditProfilePolicyId);
var openIdOptionsMonitor = provider.GetService();
astert.NotNull(openIdOptionsMonitor);
var openIdOptions = openIdOptionsMonitor.Get(AzureADB2CDefaults.OpenIdScheme);
astert.Equal("ClientId", openIdOptions.ClientId);
astert.Equal($"https://login.microsoftonline.com/tfp/domain.onmicrosoft.com/B2C_1_SiUpIn/v2.0", openIdOptions.Authority);
astert.True(openIdOptions.UseTokenLifetime);
astert.Equal("/signin-oidc", openIdOptions.CallbackPath);
astert.Equal(AzureADB2CDefaults.CookieScheme, openIdOptions.SignInScheme);
astert.NotNull(openIdOptions.TokenValidationParameters);
astert.Equal("name", openIdOptions.TokenValidationParameters.NameClaimType);
astert.NotNull(openIdOptions.Events);
var redirectHandler = openIdOptions.Events.OnRedirectToIdensatyProvider;
astert.NotNull(redirectHandler);
astert.IsType(redirectHandler.Target);
var remoteFailureHanlder = openIdOptions.Events.OnRemoteFailure;
astert.NotNull(remoteFailureHanlder);
astert.IsType(redirectHandler.Target);
}
[Fact]
public void AddAzureADB2C_ThrowsForDuplicatedSchemes()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
services.AddAuthentication()
.AddAzureADB2C(o => { })
.AddAzureADB2C(o => { });
var provider = services.BuildServiceProvider();
var azureADB2COptionsMonitor = provider.GetService();
// Act & astert
var exception = astert.Throws(
() => azureADB2COptionsMonitor.Get(AzureADB2CDefaults.AuthenticationScheme));
astert.Equal("A scheme with the name 'AzureADB2C' was already added.", exception.Message);
}
[Fact]
public void AddAzureADB2C_ThrowsWhenOpenIdSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
services.AddAuthentication()
.AddAzureADB2C(o => { })
.AddAzureADB2C("Custom", AzureADB2CDefaults.OpenIdScheme, "Cookie", null, o => { });
var provider = services.BuildServiceProvider();
var azureADB2COptionsMonitor = provider.GetService();
var expectedMessage = $"The Open ID Connect scheme 'AzureADB2COpenID' can't be astociated with the Azure Active Directory B2C scheme 'Custom'. " +
"The Open ID Connect scheme 'AzureADB2COpenID' is already mapped to the Azure Active Directory B2C scheme 'AzureADB2C'";
// Act & astert
var exception = astert.Throws(
() => azureADB2COptionsMonitor.Get(AzureADB2CDefaults.AuthenticationScheme));
astert.Equal(expectedMessage, exception.Message);
}
[Fact]
public void AddAzureADB2C_ThrowsWhenCookieSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
services.AddAuthentication()
.AddAzureADB2C(o => { })
.AddAzureADB2C("Custom", "OpenID", AzureADB2CDefaults.CookieScheme, null, o => { });
var provider = services.BuildServiceProvider();
var azureADB2COptionsMonitor = provider.GetService();
var expectedMessage = $"The cookie scheme 'AzureADB2CCookie' can't be astociated with the Azure Active Directory B2C scheme 'Custom'. " +
"The cookie scheme 'AzureADB2CCookie' is already mapped to the Azure Active Directory B2C scheme 'AzureADB2C'";
// Act & astert
var exception = astert.Throws(
() => azureADB2COptionsMonitor.Get(AzureADB2CDefaults.AuthenticationScheme));
astert.Equal(expectedMessage, exception.Message);
}
[Fact]
public void AddAzureADB2CBearer_AddsAllAuthenticationHandlers()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
// Act
services.AddAuthentication()
.AddAzureADB2CBearer(o => { });
var provider = services.BuildServiceProvider();
// astert
astert.NotNull(provider.GetService());
astert.NotNull(provider.GetService());
}
[Fact]
public void AddAzureADB2CBearer_ConfiguresAllOptions()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
// Act
services.AddAuthentication()
.AddAzureADB2CBearer(o =>
{
o.Instance = "https://login.microsoftonline.com/tfp";
o.ClientId = "ClientId";
o.CallbackPath = "/signin-oidc";
o.Domain = "domain.onmicrosoft.com";
o.SignUpSignInPolicyId = "B2C_1_SiUpIn";
});
var provider = services.BuildServiceProvider();
// astert
var azureADB2COptionsMonitor = provider.GetService();
astert.NotNull(azureADB2COptionsMonitor);
var options = azureADB2COptionsMonitor.Get(AzureADB2CDefaults.BearerAuthenticationScheme);
astert.Equal(AzureADB2CDefaults.JwtBearerAuthenticationScheme, options.JwtBearerSchemeName);
astert.Equal("https://login.microsoftonline.com/tfp", options.Instance);
astert.Equal("ClientId", options.ClientId);
astert.Equal("domain.onmicrosoft.com", options.Domain);
astert.Equal("B2C_1_SiUpIn", options.DefaultPolicy);
var bearerOptionsMonitor = provider.GetService();
astert.NotNull(bearerOptionsMonitor);
var bearerOptions = bearerOptionsMonitor.Get(AzureADB2CDefaults.JwtBearerAuthenticationScheme);
astert.Equal("ClientId", bearerOptions.Audience);
astert.Equal($"https://login.microsoftonline.com/tfp/domain.onmicrosoft.com/B2C_1_SiUpIn/v2.0", bearerOptions.Authority);
}
[Fact]
public void AddAzureADB2CBearer_ThrowsForDuplicatedSchemes()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
services.AddAuthentication()
.AddAzureADB2CBearer(o => { })
.AddAzureADB2CBearer(o => { });
var provider = services.BuildServiceProvider();
var azureADB2COptionsMonitor = provider.GetService();
// Act & astert
var exception = astert.Throws(
() => azureADB2COptionsMonitor.Get(AzureADB2CDefaults.AuthenticationScheme));
astert.Equal("A scheme with the name 'AzureADB2CBearer' was already added.", exception.Message);
}
[Fact]
public void AddAzureADB2CBearer_ThrowsWhenBearerSchemeIsAlreadyInUse()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton(new NullLoggerFactory());
services.AddAuthentication()
.AddAzureADB2CBearer(o => { })
.AddAzureADB2CBearer("Custom", AzureADB2CDefaults.JwtBearerAuthenticationScheme, o => { });
var provider = services.BuildServiceProvider();
var azureADB2COptionsMonitor = provider.GetService();
var expectedMessage = $"The JSON Web Token Bearer scheme 'AzureADB2CJwtBearer' can't be astociated with the Azure Active Directory B2C scheme 'Custom'. " +
"The JSON Web Token Bearer scheme 'AzureADB2CJwtBearer' is already mapped to the Azure Active Directory B2C scheme 'AzureADB2CBearer'";
// Act & astert
var exception = astert.Throws(
() => azureADB2COptionsMonitor.Get(AzureADB2CDefaults.AuthenticationScheme));
astert.Equal(expectedMessage, exception.Message);
}
}
}