Skip to content

Commit

Permalink
Merge pull request #51 from AzureADQuickStarts/jmprieur/GraphServiceC…
Browse files Browse the repository at this point in the history
…lient

Updating to Graph SDK 5.
  • Loading branch information
jmprieur authored Jun 16, 2023
2 parents 1e90a89 + 6350f62 commit 5a9d4e0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 42 deletions.
9 changes: 7 additions & 2 deletions AppModelv2-WebApp-OpenIDConnect-DotNet.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@
<PackageReference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform">
<Version>3.6.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph">
<Version>2.9.0</Version>
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient">
<Version>2.12.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Identity.Web.OWIN">
<Version>2.9.0</Version>
<Version>2.12.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.jQuery.Unobtrusive.Validation">
<Version>3.2.12</Version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<ActionResult> 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)
Expand Down
41 changes: 21 additions & 20 deletions AppModelv2-WebApp-OpenIDConnect-DotNet/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Protocols.OpenIdConnect" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.29.0.0" newVersion="6.29.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/>
Expand All @@ -35,7 +31,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Text.Json" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.4" newVersion="6.0.0.4"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
Expand All @@ -55,51 +51,55 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.29.0.0" newVersion="6.29.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-6.30.0.0" newVersion="6.30.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.2.2.0" newVersion="4.2.2.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.2.2.0" newVersion="4.2.2.0"/>
<assemblyIdentity name="Microsoft.Kiota.Abstractions" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Tokens" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.29.0.0" newVersion="6.29.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-6.30.0.0" newVersion="6.30.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Protocols.WsFederation" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Protocols.OpenIdConnect" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.30.0.0" newVersion="6.30.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Protocols" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.29.0.0" newVersion="6.29.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-6.30.0.0" newVersion="6.30.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Logging" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.29.0.0" newVersion="6.29.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-6.30.0.0" newVersion="6.30.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Abstractions" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.29.0.0" newVersion="6.29.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-6.30.0.0" newVersion="6.30.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Identity.Client" publicKeyToken="0A613F4DD989E8AE" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.53.0.0" newVersion="4.53.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-4.54.1.0" newVersion="4.54.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Graph" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-5.12.0.0" newVersion="5.12.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="ADB9793829DDAE60" culture="neutral"/>
Expand Down Expand Up @@ -163,12 +163,13 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Azure.Core" publicKeyToken="92742159E12E44C8" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.24.0.0" newVersion="1.24.0.0"/>
<bindingRedirect oldVersion="0.0.0.0-1.30.0.0" newVersion="1.30.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="EB42632606E9261F" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2"/>
</dependentAssembly>


</assemblyBinding>
</runtime>
Expand Down
60 changes: 44 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<OwinTokenAcquirerFactory>();

app.AddMicrosoftIdentityWebApp(factory);
factory.Services
.Configure<ConfidentialClientApplicationOptions>(options => { options.RedirectUri = "https://localhost:44368/"; })
.AddMicrosoftGraph()
.AddInMemoryTokenCaches();
factory.Build();

}
```

Expand Down Expand Up @@ -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/)
Expand Down

0 comments on commit 5a9d4e0

Please sign in to comment.