{"id":3041,"date":"2019-06-11T08:30:52","date_gmt":"2019-06-11T15:30:52","guid":{"rendered":"https:\/\/developer.microsoft.com\/en-us\/office\/blogs\/?p=3041"},"modified":"2019-06-11T08:30:52","modified_gmt":"2019-06-11T15:30:52","slug":"30daysmsgraph-upgrading-to-msal-net-v4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/30daysmsgraph-upgrading-to-msal-net-v4\/","title":{"rendered":"30DaysMSGraph \u2013 Upgrading to MSAL .NET v4"},"content":{"rendered":"<p><a href=\"https:\/\/aka.ms\/30DaysMSGraph\">List of all posts in the #30DaysMSGraph series<\/a><\/p>\n<p>We wrapped up the #30DaysMSGraph series in Nov 2018.\u00a0 Since that time there have been a few updates to the Microsoft Graph SDK as well as the Microsoft Authentication Library (MSAL).\u00a0 Notably <a href=\"https:\/\/developer.microsoft.com\/en-us\/office\/blogs\/msal-net-is-now-generally-available\/\">MSAL for .NET and JavaScript are now generally available<\/a> (GA) with v3.0.8 and <a href=\"https:\/\/developer.microsoft.com\/en-us\/identity\/blogs\/msal-net-4-0-0-is-now-available\/\">MSAL.NET 4.0.0 is now available<\/a> as well.\u00a0 This is a good opportunity to upgrade the samples in the\u00a0<a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\">dotnetcore-console-sample<\/a> repo to leverage MSAL .NET v4.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-3094\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/30DaysMSGraph_Day31_Source.jpg\" alt=\"\" width=\"701\" height=\"467\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/30DaysMSGraph_Day31_Source.jpg 960w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/30DaysMSGraph_Day31_Source-300x200.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/30DaysMSGraph_Day31_Source-768x512.jpg 768w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><\/p>\n<h2>Breaking Changes with MSAL .NETv3<\/h2>\n<p>MSAL .NET v3 (and consequently v4 also) introduces a number of <a href=\"https:\/\/github.com\/AzureAD\/microsoft-authentication-library-for-dotnet\/wiki\/MSAL.NET-3-released#breaking-changes-in-msalnet-3x\">breaking changes<\/a> when upgrading from MSAL .NET v2.\u00a0 Thankfully these changes (ex. process to instantiate a public or confidential client application, see screenshot below) are surfaced as compiler warnings (v3) or errors (v4) with additional information on how to resolve them.\u00a0 Since the projects in the\u00a0<a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\">dotnetcore-console-sample<\/a>\u00a0repo were originally based on MSAL .NET v2.x we&#8217;ll review the changes that impacted these samples.\u00a0 To avoid compiler errors when upgrading MSAL .NET, you may want to target v3.x explicitly while addressing any necessary updates to the codebase.\u00a0 Please watching the <a href=\"https:\/\/youtu.be\/16lsy7V1ki8\">March 2019 Office Hours for Microsoft Identity Platform<\/a>\u00a0and read the documentation mentioned earlier to understand how and why changes were made as well as the future plans for MSAL v4 and beyond.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-3102\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/MSALNetV4-1-1024x236.png\" alt=\"\" width=\"800\" height=\"184\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/MSALNetV4-1-1024x236.png 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/MSALNetV4-1-300x69.png 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/MSALNetV4-1-768x177.png 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2019\/06\/MSALNetV4-1.png 1046w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n<h2>Overview<\/h2>\n<p>The primary changes that need to be made include:<\/p>\n<ol>\n<li>Instantiate client application &#8211; leverage builder pattern<\/li>\n<li>Base class for client application &#8211; implement an interface<\/li>\n<li>Acquire token method &#8211; replace AcquireToken_xxx_Async(&#8230;) with AcquireToken_xxx_().ExecuteAsync()<\/li>\n<\/ol>\n<h3>Instantiate client application<\/h3>\n<p>In MSAL .NET v2, when you instantiated either a ConfidentialClientApplication or PublicClientApplication there were multiple overloads and parameters had to be specified inline.\u00a0 Now it is possible to specify parameters through a fluent syntax as well as pulling from a file \/ configuration of your own.\u00a0 In the <a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\/blob\/master\/base-console-app\/Program.cs\">Program.cs<\/a> of base-console-sample the syntax changes from<\/p>\n<pre class=\"toolbar-overlay:false toolbar-hide:false lang:default decode:true\">var cca = new ConfidentialClientApplication(clientId, authority, redirectUri, new ClientCredential(clientSecret), null, null);<\/pre>\n<p>to<\/p>\n<pre class=\"toolbar-overlay:false toolbar-hide:false toolbar-delay:false show-plain:3 lang:default decode:true\">var cca = ConfidentialClientApplicationBuilder.Create(clientId)\n             .WithAuthority(authority)\n             .WithRedirectUri(redirectUri)\n             .WithClientSecret(clientSecret)\n             .Build();<\/pre>\n<h3>Base class for client application<\/h3>\n<p>In MSAL .NET v2, client application leveraged base classes of ConfidentialClientApplication or PublicClientApplication.\u00a0 These are now shifted to interfaces of IConfidentialClientApplication or IPublicClientApplication.\u00a0 In <a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\/blob\/master\/base-console-app\/Helpers\/MsalAuthenticationProvider.cs\">MsalAuthenticationProvider.cs<\/a> of base-console-sample the private member of type ConfidentialClientApplication changes from<\/p>\n<pre class=\"toolbar-overlay:false toolbar-hide:false toolbar-delay:false show-plain:3 lang:default decode:true \">private ConfidentialClientApplication _clientApplication;\npublic MsalAuthenticationProvider(ConfidentialClientApplication clientApplication, string[] scopes)<\/pre>\n<p>to<\/p>\n<pre class=\"lang:c# decode:true\">private IConfidentialClientApplication _clientApplication;\npublic MsalAuthenticationProvider(IConfidentialClientApplication clientApplication, string[] scopes)<\/pre>\n<h3>Acquire token method<\/h3>\n<p>In MSAL .NET v2, the ConfidentialClientApplication class had numerous methods for acquiring a token and multiple overloads per method with the many optional parameters.\u00a0 Similar to the new fluent syntax for\u00a0ConfidentialClientApplicationBuilder, it is now possible to specify required parameters in the primary AcquireToken_xxx_() method with optional parameters in follow-on method calls with a final call to ExecuteAsync().\u00a0 In <a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\/blob\/master\/base-console-app\/Helpers\/MsalAuthenticationProvider.cs\">MsalAuthenticationProvider.cs<\/a> of base-console-sample the token acquisition changes from<\/p>\n<pre class=\"lang:c# decode:true \">authResult = await _clientApplication.AcquireTokenForClientAsync(_scopes);<\/pre>\n<p>to<\/p>\n<pre class=\"toolbar-overlay:false toolbar-hide:false toolbar-delay:false show-plain:3 lang:default decode:true \">authResult = await _clientApplication.AcquireTokenForClient(_scopes)\n                     .ExecuteAsync();<\/pre>\n<h2>Conclusion<\/h2>\n<p>The above changes were the minimal amount of code changes required to upgrade to MSAL .NET v3 or v4.\u00a0 Future planned changes include adopting ConfidentialClientApplicationBuilder.CreateWithOptions() to provide additional flexibility.\u00a0 When choosing which version of MSAL .NET to implement read the <a href=\"https:\/\/github.com\/AzureAD\/microsoft-authentication-library-for-dotnet\/wiki\/MSAL.NET-3-released#plans-for-deprecation-in-msalnet-3x-and-msalnet-4x\">deprecation plans<\/a>\u00a0and plan accordingly.\u00a0 We&#8217;re in the process of updating all of the <a href=\"https:\/\/github.com\/microsoftgraph\/dotnetcore-console-sample\">dotnetcore-console-sample<\/a>\u00a0code samples and documentation to leverage MSAL .NET v4.\u00a0 If you find any samples or docs that are incorrect or do not work as expected please file an issue.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We wrapped up the #30DaysMSGraph series in Nov 2018.\u00a0 Since that time there have been a few updates to the Microsoft Graph SDK as well as the Microsoft Authentication Library (MSAL).\u00a0 Notably MSAL for .Net and JavaScript are now generally available (GA) with v3.0.8 and MSAL.Net 4.0.0 is now available as well.\u00a0 This is a good opportunity to upgrade the samples in the\u00a0dotnetcore-console-sample repo to leverage MSAL .Net v4.<\/p>\n","protected":false},"author":73055,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[84],"class_list":["post-3041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-30daysmsgraph"],"acf":[],"blog_post_summary":"<p>We wrapped up the #30DaysMSGraph series in Nov 2018.\u00a0 Since that time there have been a few updates to the Microsoft Graph SDK as well as the Microsoft Authentication Library (MSAL).\u00a0 Notably MSAL for .Net and JavaScript are now generally available (GA) with v3.0.8 and MSAL.Net 4.0.0 is now available as well.\u00a0 This is a good opportunity to upgrade the samples in the\u00a0dotnetcore-console-sample repo to leverage MSAL .Net v4.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/3041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/73055"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=3041"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/3041\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=3041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=3041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=3041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}