|
1 |
| -// Copyright (c) .NET Foundation. All rights reserved. |
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.using Microsoft.AspNetCore.Authorization;
|
3 | 3 |
|
4 | 4 | using System;
|
@@ -76,6 +76,98 @@ public void AddAzureAD_ConfiguresAllOptions()
|
76 | 76 | Assert.Equal(AzureADDefaults.CookieScheme, openIdOptions.SignInScheme);
|
77 | 77 | }
|
78 | 78 |
|
| 79 | + [Fact] |
| 80 | + public void AddAzureAD_AllowsOverridingCookiesAndOpenIdConnectSettings() |
| 81 | + { |
| 82 | + // Arrange |
| 83 | + var services = new ServiceCollection(); |
| 84 | + services.AddSingleton<ILoggerFactory>(new NullLoggerFactory()); |
| 85 | + |
| 86 | + // Act |
| 87 | + services.AddAuthentication() |
| 88 | + .AddAzureAD(o => |
| 89 | + { |
| 90 | + o.Instance = "https://login.microsoftonline.com"; |
| 91 | + o.ClientId = "ClientId"; |
| 92 | + o.ClientSecret = "ClientSecret"; |
| 93 | + o.CallbackPath = "/signin-oidc"; |
| 94 | + o.Domain = "domain.onmicrosoft.com"; |
| 95 | + o.TenantId = "Common"; |
| 96 | + }); |
| 97 | + |
| 98 | + services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, o => |
| 99 | + { |
| 100 | + o.Authority = "https://overriden.com"; |
| 101 | + }); |
| 102 | + |
| 103 | + services.Configure<CookieAuthenticationOptions>(AzureADDefaults.CookieScheme, o => |
| 104 | + { |
| 105 | + o.AccessDeniedPath = "/Overriden"; |
| 106 | + }); |
| 107 | + |
| 108 | + var provider = services.BuildServiceProvider(); |
| 109 | + |
| 110 | + // Assert |
| 111 | + var openIdOptionsMonitor = provider.GetService<IOptionsMonitor<OpenIdConnectOptions>>(); |
| 112 | + Assert.NotNull(openIdOptionsMonitor); |
| 113 | + var openIdOptions = openIdOptionsMonitor.Get(AzureADDefaults.OpenIdScheme); |
| 114 | + Assert.Equal("ClientId", openIdOptions.ClientId); |
| 115 | + Assert.Equal($"https://overriden.com", openIdOptions.Authority); |
| 116 | + |
| 117 | + var cookieAuthenticationOptionsMonitor = provider.GetService<IOptionsMonitor<CookieAuthenticationOptions>>(); |
| 118 | + Assert.NotNull(cookieAuthenticationOptionsMonitor); |
| 119 | + var cookieAuthenticationOptions = cookieAuthenticationOptionsMonitor.Get(AzureADDefaults.CookieScheme); |
| 120 | + Assert.Equal("/AzureAD/Account/SignIn/AzureAD", cookieAuthenticationOptions.LoginPath); |
| 121 | + Assert.Equal("/Overriden", cookieAuthenticationOptions.AccessDeniedPath); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void AddAzureAD_RegisteringAddCookiesAndAddOpenIdConnectHasNoImpactOnAzureAAExtensions() |
| 126 | + { |
| 127 | + // Arrange |
| 128 | + var services = new ServiceCollection(); |
| 129 | + services.AddSingleton<ILoggerFactory>(new NullLoggerFactory()); |
| 130 | + |
| 131 | + // Act |
| 132 | + services.AddAuthentication() |
| 133 | + .AddOpenIdConnect() |
| 134 | + .AddCookie() |
| 135 | + .AddAzureAD(o => |
| 136 | + { |
| 137 | + o.Instance = "https://login.microsoftonline.com"; |
| 138 | + o.ClientId = "ClientId"; |
| 139 | + o.ClientSecret = "ClientSecret"; |
| 140 | + o.CallbackPath = "/signin-oidc"; |
| 141 | + o.Domain = "domain.onmicrosoft.com"; |
| 142 | + o.TenantId = "Common"; |
| 143 | + }); |
| 144 | + |
| 145 | + services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, o => |
| 146 | + { |
| 147 | + o.Authority = "https://overriden.com"; |
| 148 | + }); |
| 149 | + |
| 150 | + services.Configure<CookieAuthenticationOptions>(AzureADDefaults.CookieScheme, o => |
| 151 | + { |
| 152 | + o.AccessDeniedPath = "/Overriden"; |
| 153 | + }); |
| 154 | + |
| 155 | + var provider = services.BuildServiceProvider(); |
| 156 | + |
| 157 | + // Assert |
| 158 | + var openIdOptionsMonitor = provider.GetService<IOptionsMonitor<OpenIdConnectOptions>>(); |
| 159 | + Assert.NotNull(openIdOptionsMonitor); |
| 160 | + var openIdOptions = openIdOptionsMonitor.Get(AzureADDefaults.OpenIdScheme); |
| 161 | + Assert.Equal("ClientId", openIdOptions.ClientId); |
| 162 | + Assert.Equal($"https://overriden.com", openIdOptions.Authority); |
| 163 | + |
| 164 | + var cookieAuthenticationOptionsMonitor = provider.GetService<IOptionsMonitor<CookieAuthenticationOptions>>(); |
| 165 | + Assert.NotNull(cookieAuthenticationOptionsMonitor); |
| 166 | + var cookieAuthenticationOptions = cookieAuthenticationOptionsMonitor.Get(AzureADDefaults.CookieScheme); |
| 167 | + Assert.Equal("/AzureAD/Account/SignIn/AzureAD", cookieAuthenticationOptions.LoginPath); |
| 168 | + Assert.Equal("/Overriden", cookieAuthenticationOptions.AccessDeniedPath); |
| 169 | + } |
| 170 | + |
79 | 171 | [Fact]
|
80 | 172 | public void AddAzureAD_ThrowsForDuplicatedSchemes()
|
81 | 173 | {
|
@@ -197,6 +289,73 @@ public void AddAzureADBearer_ConfiguresAllOptions()
|
197 | 289 | Assert.Equal($"https://login.microsoftonline.com/TenantId", bearerOptions.Authority);
|
198 | 290 | }
|
199 | 291 |
|
| 292 | + [Fact] |
| 293 | + public void AddAzureADBearer_CanOverrideJwtBearerOptionsConfiguration() |
| 294 | + { |
| 295 | + // Arrange |
| 296 | + var services = new ServiceCollection(); |
| 297 | + services.AddSingleton<ILoggerFactory>(new NullLoggerFactory()); |
| 298 | + |
| 299 | + // Act |
| 300 | + services.AddAuthentication() |
| 301 | + .AddAzureADBearer(o => |
| 302 | + { |
| 303 | + o.Instance = "https://login.microsoftonline.com/"; |
| 304 | + o.ClientId = "ClientId"; |
| 305 | + o.CallbackPath = "/signin-oidc"; |
| 306 | + o.Domain = "domain.onmicrosoft.com"; |
| 307 | + o.TenantId = "TenantId"; |
| 308 | + }); |
| 309 | + |
| 310 | + services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, o => |
| 311 | + { |
| 312 | + o.Audience = "http://overriden.com"; |
| 313 | + }); |
| 314 | + |
| 315 | + var provider = services.BuildServiceProvider(); |
| 316 | + |
| 317 | + // Assert |
| 318 | + var bearerOptionsMonitor = provider.GetService<IOptionsMonitor<JwtBearerOptions>>(); |
| 319 | + Assert.NotNull(bearerOptionsMonitor); |
| 320 | + var bearerOptions = bearerOptionsMonitor.Get(AzureADDefaults.JwtBearerAuthenticationScheme); |
| 321 | + Assert.Equal("http://overriden.com", bearerOptions.Audience); |
| 322 | + Assert.Equal($"https://login.microsoftonline.com/TenantId", bearerOptions.Authority); |
| 323 | + } |
| 324 | + |
| 325 | + [Fact] |
| 326 | + public void AddAzureADBearer_RegisteringJwtBearerHasNoImpactOnAzureAAExtensions() |
| 327 | + { |
| 328 | + // Arrange |
| 329 | + var services = new ServiceCollection(); |
| 330 | + services.AddSingleton<ILoggerFactory>(new NullLoggerFactory()); |
| 331 | + |
| 332 | + // Act |
| 333 | + services.AddAuthentication() |
| 334 | + .AddJwtBearer() |
| 335 | + .AddAzureADBearer(o => |
| 336 | + { |
| 337 | + o.Instance = "https://login.microsoftonline.com/"; |
| 338 | + o.ClientId = "ClientId"; |
| 339 | + o.CallbackPath = "/signin-oidc"; |
| 340 | + o.Domain = "domain.onmicrosoft.com"; |
| 341 | + o.TenantId = "TenantId"; |
| 342 | + }); |
| 343 | + |
| 344 | + services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, o => |
| 345 | + { |
| 346 | + o.Audience = "http://overriden.com"; |
| 347 | + }); |
| 348 | + |
| 349 | + var provider = services.BuildServiceProvider(); |
| 350 | + |
| 351 | + // Assert |
| 352 | + var bearerOptionsMonitor = provider.GetService<IOptionsMonitor<JwtBearerOptions>>(); |
| 353 | + Assert.NotNull(bearerOptionsMonitor); |
| 354 | + var bearerOptions = bearerOptionsMonitor.Get(AzureADDefaults.JwtBearerAuthenticationScheme); |
| 355 | + Assert.Equal("http://overriden.com", bearerOptions.Audience); |
| 356 | + Assert.Equal($"https://login.microsoftonline.com/TenantId", bearerOptions.Authority); |
| 357 | + } |
| 358 | + |
200 | 359 | [Fact]
|
201 | 360 | public void AddAzureADBearer_ThrowsForDuplicatedSchemes()
|
202 | 361 | {
|
|
0 commit comments