From f0ff3b64c7e640d987fa9ede6177b08147c9fc94 Mon Sep 17 00:00:00 2001 From: Mark Szabo Date: Sun, 15 Jul 2018 18:14:43 +0200 Subject: [PATCH] Fix #15 User.Identity.Name was always null --- .../Controllers/HomeController.cs | 2 +- .../Extensions/AzureAdAuthenticationBuilderExtensions.cs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/MicrosoftGraphAspNetCoreConnectSample/Controllers/HomeController.cs b/MicrosoftGraphAspNetCoreConnectSample/Controllers/HomeController.cs index 5d4e9f8..3c4b7cd 100644 --- a/MicrosoftGraphAspNetCoreConnectSample/Controllers/HomeController.cs +++ b/MicrosoftGraphAspNetCoreConnectSample/Controllers/HomeController.cs @@ -33,7 +33,7 @@ public async Task Index(string email) if (User.Identity.IsAuthenticated) { // Get users's email. - email = email ?? User.Identity.Name ?? User.FindFirst("preferred_username").Value; + email = email ?? User.FindFirst("preferred_username")?.Value; ViewData["Email"] = email; // Get user's id for token cache. diff --git a/MicrosoftGraphAspNetCoreConnectSample/Extensions/AzureAdAuthenticationBuilderExtensions.cs b/MicrosoftGraphAspNetCoreConnectSample/Extensions/AzureAdAuthenticationBuilderExtensions.cs index 254f216..6ab435b 100644 --- a/MicrosoftGraphAspNetCoreConnectSample/Extensions/AzureAdAuthenticationBuilderExtensions.cs +++ b/MicrosoftGraphAspNetCoreConnectSample/Extensions/AzureAdAuthenticationBuilderExtensions.cs @@ -49,6 +49,9 @@ public void Configure(string name, OpenIdConnectOptions options) options.TokenValidationParameters = new TokenValidationParameters { + // Ensure that User.Identity.Name is set correctly after login + NameClaimType = "name", + // Instead of using the default validation (validating against a single issuer value, as we do in line of business apps), // we inject our own multitenant validation logic ValidateIssuer = false,