Ensure AzureADOptions.Instance is set (#9967) Fixes #6022

This commit is contained in:
Mickaël Derriey 2019-05-17 01:24:12 +10:00 committed by Chris Ross
parent 83eeeec10c
commit 3da1b107ef
3 changed files with 69 additions and 1 deletions

View File

@ -60,6 +60,8 @@ namespace Microsoft.AspNetCore.Authentication
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AzureADOptions>, AzureADOptionsConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<AzureADOptions>, AzureADOptionsValidation>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<JwtBearerOptions>, AzureADJwtBearerOptionsConfiguration>());
builder.Services.Configure(scheme, configureOptions);
@ -115,6 +117,8 @@ namespace Microsoft.AspNetCore.Authentication
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AzureADOptions>, AzureADOptionsConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<AzureADOptions>, AzureADOptionsValidation>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<OpenIdConnectOptions>, AzureADOpenIdConnectOptionsConfiguration>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<CookieAuthenticationOptions>, AzureADCookieOptionsConfiguration>());

View File

@ -0,0 +1,20 @@
// 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.Extensions.Options;
namespace Microsoft.AspNetCore.Authentication.AzureAD.UI
{
internal class AzureADOptionsValidation : IValidateOptions<AzureADOptions>
{
public ValidateOptionsResult Validate(string name, AzureADOptions options)
{
if (string.IsNullOrEmpty(options.Instance))
{
return ValidateOptionsResult.Fail($"The '{nameof(AzureADOptions.Instance)}' option must be provided.");
}
return ValidateOptionsResult.Success;
}
}
}

View File

@ -2,10 +2,10 @@
// 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.AzureAD.UI;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -237,6 +237,28 @@ namespace Microsoft.AspNetCore.Authentication
Assert.Equal(expectedMessage, exception.Message);
}
[Fact]
public void AddAzureAD_ThrowsWhenInstanceIsNotSet()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddAuthentication()
.AddAzureAD(o => { });
var provider = services.BuildServiceProvider();
var azureADOptionsMonitor = provider.GetService<IOptionsMonitor<AzureADOptions>>();
var expectedMessage = "The 'Instance' option must be provided.";
// Act & Assert
var exception = Assert.Throws<OptionsValidationException>(
() => azureADOptionsMonitor.Get(AzureADDefaults.AuthenticationScheme));
Assert.Contains(expectedMessage, exception.Failures);
}
[Fact]
public void AddAzureADBearer_AddsAllAuthenticationHandlers()
{
@ -400,5 +422,27 @@ namespace Microsoft.AspNetCore.Authentication
Assert.Equal(expectedMessage, exception.Message);
}
[Fact]
public void AddAzureADBearer_ThrowsWhenInstanceIsNotSet()
{
// Arrange
var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
services.AddAuthentication()
.AddAzureADBearer(o => { });
var provider = services.BuildServiceProvider();
var azureADOptionsMonitor = provider.GetService<IOptionsMonitor<AzureADOptions>>();
var expectedMessage = "The 'Instance' option must be provided.";
// Act & Assert
var exception = Assert.Throws<OptionsValidationException>(
() => azureADOptionsMonitor.Get(AzureADDefaults.AuthenticationScheme));
Assert.Contains(expectedMessage, exception.Failures);
}
}
}