Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle anonymous controller #1413

Merged
merged 2 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Microsoft.Identity.Web/TokenAcquisition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
Expand All @@ -41,7 +43,7 @@ internal partial class TokenAcquisition : ITokenAcquisitionInternal
/// Please call GetOrBuildConfidentialClientApplication instead of accessing this field directly.
/// </summary>
private IConfidentialClientApplication? _application;
private bool retryClientCertificate;
private bool _retryClientCertificate;
private readonly IHttpContextAccessor _httpContextAccessor;
private HttpContext? CurrentHttpContext => _httpContextAccessor.HttpContext;
private readonly IMsalHttpClientFactory _httpClientFactory;
Expand Down Expand Up @@ -194,7 +196,7 @@ public async Task AddAccountToCacheFromAuthorizationCodeAsync(
_application = null;

// Retry
retryClientCertificate = true;
_retryClientCertificate = true;
await AddAccountToCacheFromAuthorizationCodeAsync(context, scopes, authenticationScheme).ConfigureAwait(false);
}
catch (MsalException ex)
Expand All @@ -204,7 +206,7 @@ public async Task AddAccountToCacheFromAuthorizationCodeAsync(
}
finally
{
retryClientCertificate = false;
_retryClientCertificate = false;
}
}

Expand Down Expand Up @@ -290,7 +292,7 @@ public async Task<AuthenticationResult> GetAuthenticationResultForUserAsync(
_application = null;

// Retry
retryClientCertificate = true;
_retryClientCertificate = true;
return await GetAuthenticationResultForUserAsync(scopes, tenantId: tenantId, userFlow: userFlow, user: user, tokenAcquisitionOptions: tokenAcquisitionOptions).ConfigureAwait(false);
}
catch (MsalUiRequiredException ex)
Expand All @@ -304,7 +306,7 @@ public async Task<AuthenticationResult> GetAuthenticationResultForUserAsync(
}
finally
{
retryClientCertificate = false;
_retryClientCertificate = false;
}
}

Expand Down Expand Up @@ -397,12 +399,12 @@ public Task<AuthenticationResult> GetAuthenticationResultForAppAsync(
_application = null;

// Retry
retryClientCertificate = true;
_retryClientCertificate = true;
return GetAuthenticationResultForAppAsync(scope, tenant: tenant, tokenAcquisitionOptions: tokenAcquisitionOptions);
}
finally
{
retryClientCertificate = false;
_retryClientCertificate = false;
}
}

Expand Down Expand Up @@ -598,14 +600,15 @@ public string GetEffectiveAuthenticationScheme(string? authenticationScheme)
}
else
{
return (CurrentHttpContext?.GetTokenUsedToCallWebAPI() != null)
? JwtBearerDefaults.AuthenticationScheme : OpenIdConnectDefaults.AuthenticationScheme;
return _serviceProvider.GetService<IAuthenticationSchemeProvider>()?.GetDefaultAuthenticateSchemeAsync()?.Result?.Name ??
((CurrentHttpContext?.GetTokenUsedToCallWebAPI() != null)
? JwtBearerDefaults.AuthenticationScheme : OpenIdConnectDefaults.AuthenticationScheme);
}
}

private bool IsInvalidClientCertificateError(MsalServiceException exMsal)
{
return !retryClientCertificate &&
return !_retryClientCertificate &&
string.Equals(exMsal.ErrorCode, Constants.InvalidClient, StringComparison.OrdinalIgnoreCase) &&
exMsal.Message.Contains(Constants.InvalidKeyError, StringComparison.OrdinalIgnoreCase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Globalization;
using System.Net.Http;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -35,7 +37,6 @@ private void InitializeTokenAcquisitionObjects()
_provider.GetService<IHttpClientFactory>(),
_provider.GetService<ILogger<TokenAcquisition>>(),
_provider);
_tokenAcquisition.GetOptions(OpenIdConnectDefaults.AuthenticationScheme);
}

private void BuildTheRequiredServices()
Expand All @@ -45,9 +46,10 @@ private void BuildTheRequiredServices()
provider => _microsoftIdentityOptionsMonitor);
services.AddTransient(
provider => _applicationOptionsMonitor);
services.Configure<MergedOptions>(OpenIdConnectDefaults.AuthenticationScheme, options => { });
services.Configure<MergedOptions>(options => { });
services.AddTokenAcquisition();
services.AddLogging();
services.AddAuthentication();
_provider = services.BuildServiceProvider();
}

Expand Down Expand Up @@ -93,6 +95,25 @@ public void VerifyCorrectAuthorityUsedInTokenAcquisitionTests(string tenant)
}
}

[Theory]
[InlineData(JwtBearerDefaults.AuthenticationScheme)]
[InlineData(OpenIdConnectDefaults.AuthenticationScheme)]
[InlineData(null)]
public void VerifyCorrectSchemeTests(string scheme)
{
BuildTheRequiredServices();
InitializeTokenAcquisitionObjects();

if (!string.IsNullOrEmpty(scheme))
{
Assert.Equal(scheme, _tokenAcquisition.GetEffectiveAuthenticationScheme(scheme));
}
else
{
Assert.Equal(OpenIdConnectDefaults.AuthenticationScheme, _tokenAcquisition.GetEffectiveAuthenticationScheme(scheme));
}
}

[Theory]
[InlineData(TestConstants.B2CInstance)]
[InlineData(TestConstants.B2CLoginMicrosoft)]
Expand Down