Skip to content

Commit b991e4b

Browse files
authored
Fix AzureAd options validation (#13480)
* Skip getting AzureAdOptions for not AzureADUi Cookies scheme #13311 (#13327) * Also check Azure Jwt options for #13311
1 parent ecae683 commit b991e4b

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/Azure/AzureAD/Authentication.AzureAD.UI/src/AzureADCookieOptionsConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public AzureADCookieOptionsConfiguration(IOptions<AzureADSchemeOptions> schemeOp
2121
public void Configure(string name, CookieAuthenticationOptions options)
2222
{
2323
var AzureADScheme = GetAzureADScheme(name);
24+
if (AzureADScheme is null)
25+
{
26+
return;
27+
}
28+
2429
var AzureADOptions = _AzureADOptions.Get(AzureADScheme);
2530
if (name != AzureADOptions.CookieSchemeName)
2631
{

src/Azure/AzureAD/Authentication.AzureAD.UI/src/AzureADJwtBearerOptionsConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public AzureADJwtBearerOptionsConfiguration(
2424
public void Configure(string name, JwtBearerOptions options)
2525
{
2626
var azureADScheme = GetAzureADScheme(name);
27+
if (azureADScheme is null)
28+
{
29+
return;
30+
}
31+
2732
var azureADOptions = _azureADOptions.Get(azureADScheme);
2833
if (name != azureADOptions.JwtBearerSchemeName)
2934
{

src/Azure/AzureAD/Authentication.AzureAD.UI/test/AzureADAuthenticationBuilderExtensionsTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,22 @@ public void AddAzureAD_ThrowsWhenInstanceIsNotSet()
268268
Assert.Contains(expectedMessage, exception.Failures);
269269
}
270270

271+
[Fact]
272+
public void AddAzureAD_SkipsOptionsValidationForNonAzureCookies()
273+
{
274+
var services = new ServiceCollection();
275+
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
276+
277+
services.AddAuthentication()
278+
.AddAzureAD(o => { })
279+
.AddCookie("other");
280+
281+
var provider = services.BuildServiceProvider();
282+
var cookieAuthOptions = provider.GetService<IOptionsMonitor<CookieAuthenticationOptions>>();
283+
284+
Assert.NotNull(cookieAuthOptions.Get("other"));
285+
}
286+
271287
[Fact]
272288
public void AddAzureADBearer_AddsAllAuthenticationHandlers()
273289
{
@@ -453,5 +469,21 @@ public void AddAzureADBearer_ThrowsWhenInstanceIsNotSet()
453469

454470
Assert.Contains(expectedMessage, exception.Failures);
455471
}
472+
473+
[Fact]
474+
public void AddAzureADBearer_SkipsOptionsValidationForNonAzureCookies()
475+
{
476+
var services = new ServiceCollection();
477+
services.AddSingleton<ILoggerFactory>(new NullLoggerFactory());
478+
479+
services.AddAuthentication()
480+
.AddAzureADBearer(o => { })
481+
.AddJwtBearer("other", o => { });
482+
483+
var provider = services.BuildServiceProvider();
484+
var jwtOptions = provider.GetService<IOptionsMonitor<JwtBearerOptions>>();
485+
486+
Assert.NotNull(jwtOptions.Get("other"));
487+
}
456488
}
457489
}

0 commit comments

Comments
 (0)