From 6350f6277a5ef6fceb6994257f43c80fdfa2dd56 Mon Sep 17 00:00:00 2001 From: Jean-Marc Prieur Date: Fri, 16 Jun 2023 16:39:23 -0700 Subject: [PATCH] Updating to Graph SDK 5. --- AppModelv2-WebApp-OpenIDConnect-DotNet.sln | 9 ++- ...Modelv2-WebApp-OpenIDConnect-DotNet.csproj | 6 +- .../Controllers/ClaimsController.cs | 2 +- .../Web.config | 41 ++++++------- README.md | 60 ++++++++++++++----- 5 files changed, 76 insertions(+), 42 deletions(-) diff --git a/AppModelv2-WebApp-OpenIDConnect-DotNet.sln b/AppModelv2-WebApp-OpenIDConnect-DotNet.sln index ea77426..4f9ac91 100644 --- a/AppModelv2-WebApp-OpenIDConnect-DotNet.sln +++ b/AppModelv2-WebApp-OpenIDConnect-DotNet.sln @@ -1,10 +1,15 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30717.126 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33801.468 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppModelv2-WebApp-OpenIDConnect-DotNet", "AppModelv2-WebApp-OpenIDConnect-DotNet\AppModelv2-WebApp-OpenIDConnect-DotNet.csproj", "{B1683E5F-00E4-4690-97AE-1C94A4F9FC82}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{588817A7-F55A-40B2-A012-681529DF1AEB}" + ProjectSection(SolutionItems) = preProject + README.md = README.md + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/AppModelv2-WebApp-OpenIDConnect-DotNet/AppModelv2-WebApp-OpenIDConnect-DotNet.csproj b/AppModelv2-WebApp-OpenIDConnect-DotNet/AppModelv2-WebApp-OpenIDConnect-DotNet.csproj index 5ff9da6..1a16aa5 100644 --- a/AppModelv2-WebApp-OpenIDConnect-DotNet/AppModelv2-WebApp-OpenIDConnect-DotNet.csproj +++ b/AppModelv2-WebApp-OpenIDConnect-DotNet/AppModelv2-WebApp-OpenIDConnect-DotNet.csproj @@ -129,11 +129,11 @@ 3.6.0 - - 2.9.0 + + 2.12.2 - 2.9.0 + 2.12.2 3.2.12 diff --git a/AppModelv2-WebApp-OpenIDConnect-DotNet/Controllers/ClaimsController.cs b/AppModelv2-WebApp-OpenIDConnect-DotNet/Controllers/ClaimsController.cs index ae54406..63a2606 100644 --- a/AppModelv2-WebApp-OpenIDConnect-DotNet/Controllers/ClaimsController.cs +++ b/AppModelv2-WebApp-OpenIDConnect-DotNet/Controllers/ClaimsController.cs @@ -30,7 +30,7 @@ public async Task Index() // You can also call Microsoft Graph (with incremental consent) try { - var me = await this.GetGraphServiceClient().Me.Request().GetAsync(); + var me = await this.GetGraphServiceClient().Me.GetAsync(); ViewBag.Username = me.DisplayName; } catch (ServiceException graphEx) when (graphEx.InnerException is MicrosoftIdentityWebChallengeUserException) diff --git a/AppModelv2-WebApp-OpenIDConnect-DotNet/Web.config b/AppModelv2-WebApp-OpenIDConnect-DotNet/Web.config index feaf7f8..b7c7616 100644 --- a/AppModelv2-WebApp-OpenIDConnect-DotNet/Web.config +++ b/AppModelv2-WebApp-OpenIDConnect-DotNet/Web.config @@ -21,10 +21,6 @@ - - - - @@ -35,7 +31,7 @@ - + @@ -55,51 +51,55 @@ - + - + - + - - - - - - + + - + + + + + - + - + - + - + + + + + @@ -163,12 +163,13 @@ - + + diff --git a/README.md b/README.md index 6c1fbe5..9ecc199 100644 --- a/README.md +++ b/README.md @@ -153,22 +153,16 @@ Were we successful in addressing your learning objective? [Do consider taking a ```csharp public void Configuration(IAppBuilder app) { - ... - app.UseOpenIdConnectAuthentication( - new OpenIdConnectAuthenticationOptions - { - ClientId = clientId, - Authority = authority, - RedirectUri = redirectUri, - PostLogoutRedirectUri = redirectUri, - Scope = OpenIdConnectScope.OpenIdProfile, - ResponseType = OpenIdConnectResponseType.CodeIdToken, - Notifications = new OpenIdConnectAuthenticationNotifications - { - AuthenticationFailed = OnAuthenticationFailed - } - } - ); + /// ... + OwinTokenAcquirerFactory factory = TokenAcquirerFactory.GetDefaultInstance(); + + app.AddMicrosoftIdentityWebApp(factory); + factory.Services + .Configure(options => { options.RedirectUri = "https://localhost:44368/"; }) + .AddMicrosoftGraph() + .AddInMemoryTokenCaches(); + factory.Build(); + } ``` @@ -196,6 +190,40 @@ Were we successful in addressing your learning objective? [Do consider taking a } ``` +1. `ClaimsController` shows how to access the claims in the ID token + ```csharp + public ActionResult Index() + { + var userClaims = User.Identity as System.Security.Claims.ClaimsIdentity; + + // You get the user’s first and last name below: + ViewBag.Name = userClaims?.FindFirst("name")?.Value; + + // The subject/ NameIdentifier claim can be used to uniquely identify the user across the web + ViewBag.Subject = userClaims?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value; + + // TenantId is the unique Tenant Id - which represents an organization in Azure AD + ViewBag.TenantId = userClaims?.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid")?.Value; + } + ``` + +1. It also shows how to call Microsoft Graph, with incremental consent (the user will need to consent + to more scopes if needed. + + ```csharp + // You can also call Microsoft Graph (with incremental consent) + try + { + var me = await this.GetGraphServiceClient().Me.GetAsync(); + ViewBag.Username = me.DisplayName; + } + catch (ServiceException graphEx) when (graphEx.InnerException is MicrosoftIdentityWebChallengeUserException) + { + HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType); + return View(); + } + ``` + ## More information - [Microsoft identity platform (Azure Active Directory for developers)](https://docs.microsoft.com/azure/active-directory/develop/)