{"id":14985,"date":"2017-10-08T20:58:00","date_gmt":"2017-10-08T20:58:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=14985"},"modified":"2019-03-04T15:11:44","modified_gmt":"2019-03-04T22:11:44","slug":"using-adal-with-angular2-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/using-adal-with-angular2-part-2\/","title":{"rendered":"Using ADAL with Angular2\u2013Part 2"},"content":{"rendered":"<p>In this post,\u00a0 Senior Application Development Manager, <a href=\"https:\/\/www.linkedin.com\/in\/vsaroopchand\/\">Vishal Saroopchand<\/a> who follows up his first post to demonstrate how to pass and revalidate the JWT token.<\/p>\n<hr \/>\n<p>In my <a href=\"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/2017\/04\/26\/using-adal-with-angular2\/\">last<\/a> blog posting, I explained how to use Adaj.js with Angular2 and demonstrated how to retrieve the JWT token from AAD. I also stated you can supply the JWT to your back end WebAPI services. This blank statement caused several inquiries on how to pass the JWT from client\u2019s HTTP requests and revalidating on the WebAPI service.<\/p>\n<p>In this post, I have updated both the original source and added two WebAPI implementation that shows how to pass and revalidate the JWT token.<\/p>\n<p>One implementation is for ASP.NET MVC, the other ASP.NET Core.<\/p>\n<p><b>Here are the key points:<\/b><\/p>\n<h3>Client App<\/h3>\n<p>You must supply the JWT token as a Bearer token before invoking HTTP requests. You do this by constructing a <a href=\"https:\/\/angular.io\/api\/http\/Headers\">Headers<\/a> collection and adding an Authorization header.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal1.png\"><img decoding=\"async\" title=\"adal1\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal1_thumb.png\" alt=\"adal1\" width=\"1028\" height=\"158\" border=\"0\" \/><\/a><\/p>\n<p>You then supply the headers in the optional <a href=\"https:\/\/angular.io\/api\/http\/RequestOptionsArgs\">RequestOptionsArgs<\/a> parameter on <a href=\"https:\/\/angular.io\/api\/http\/Http\">Http<\/a> Service GET\/PUT\/POST\/DELETE calls<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal2.png\"><img decoding=\"async\" title=\"adal2\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal2_thumb.png\" alt=\"adal2\" width=\"889\" height=\"772\" border=\"0\" \/><\/a><\/p>\n<h3>AspNetCore<\/h3>\n<p>Install and configure CORS middleware (change the Origins, Headers and Methods to meet your security needs), Microsoft.AspNetCore.Mvc.Cors<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal3.png\"><img decoding=\"async\" title=\"adal3\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal3_thumb.png\" alt=\"adal3\" width=\"644\" height=\"285\" border=\"0\" \/><\/a><\/p>\n<p>Install and configure JwtBearer Authentication middleware (change settings to meet your security needs), Microsoft.IdentityModel.Clients.ActiveDirectory<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal4.png\"><img decoding=\"async\" title=\"adal4\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal4_thumb.png\" alt=\"adal4\" width=\"973\" height=\"772\" border=\"0\" \/><\/a><\/p>\n<p>You will also need the following middleware:<\/p>\n<pre>Microsoft.AspNetCore.Authentication.JwtBearer\r\nMicrosoft.IdentityModel.Tokens\r\n<\/pre>\n<p>Finally, secure your controllers by adding the Authorize attribute<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal5.png\"><img decoding=\"async\" title=\"adal5\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal5_thumb.png\" alt=\"adal5\" width=\"1028\" height=\"117\" border=\"0\" \/><\/a><\/p>\n<h3>AspNet5<\/h3>\n<p>Install the following NuGet packages<\/p>\n<pre class=\"lang:default decode:true\">&lt;package id=\"Owin\" version=\"1.0\" targetFramework=\"net462\" \/&gt;\r\n&lt;package id=\"Microsoft.Owin\" version=\"3.1.0\" targetFramework=\"net462\" \/&gt;\r\n&lt;package id=\"Microsoft.Owin.Host.SystemWeb\" version=\"3.1.0\" targetFramework=\"net462\" \/&gt;\r\n&lt;package id=\"Microsoft.Owin.Security\" version=\"3.1.0\" targetFramework=\"net462\" \/&gt;\r\n&lt;package id=\"Microsoft.Owin.Security.ActiveDirectory\" version=\"3.1.0\" targetFramework=\"net462\" \/&gt;\r\n&lt;package id=\"Microsoft.Owin.Security.Jwt\" version=\"3.1.0\" targetFramework=\"net462\" \/&gt;\r\n&lt;package id=\"Microsoft.Owin.Security.OAuth\" version=\"3.1.0\" targetFramework=\"net462\" \/&gt;\r\n<\/pre>\n<p>Add Startup class and configure AAD Authentication middleware. You will need to retrieve Tenant and ClientIDs from AppSettings.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal6.png\"><img decoding=\"async\" title=\"adal6\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal6_thumb.png\" alt=\"adal6\" width=\"932\" height=\"772\" border=\"0\" \/><\/a><\/p>\n<p>Secure API controllers by adding the Authorize attribute.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal7.png\"><img decoding=\"async\" title=\"adal7\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/31\/2019\/04\/adal7_thumb.png\" alt=\"adal7\" width=\"644\" height=\"94\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>You will find the <strong>updated client app code<\/strong> <a href=\"https:\/\/github.com\/vsaroopchand\/angular2-seed\">here<\/a> and the services <a href=\"https:\/\/github.com\/vsaroopchand\/angular2-seed-server\">here<\/a>.<\/p>\n<hr \/>\n<p><a href=\"https:\/\/blogs.msdn.com\/b\/premier_developer\/archive\/2014\/09\/15\/welcome.aspx\"><strong>Premier Support for Developers<\/strong><\/a> provides strategic technology guidance, critical support coverage, and a range of essential services to help teams optimize development lifecycles and improve software quality.\u00a0 Contact your Application Development Manager (ADM) or <a href=\"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/contact-us\/\">email us<\/a><b><\/b> to learn more about what we can do for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post,\u00a0 Senior Application Development Manager, Vishal Saroopchand who follows up his first post to demonstrate how to pass and revalidate the JWT token. In my last blog posting, I explained how to use Adaj.js with Angular2 and demonstrated how to retrieve the JWT token from AAD. I also stated you can supply the [&hellip;]<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,51,3],"class_list":["post-14985","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-permierdev","tag-adal","tag-angular","tag-team"],"acf":[],"blog_post_summary":"<p>In this post,\u00a0 Senior Application Development Manager, Vishal Saroopchand who follows up his first post to demonstrate how to pass and revalidate the JWT token. In my last blog posting, I explained how to use Adaj.js with Angular2 and demonstrated how to retrieve the JWT token from AAD. I also stated you can supply the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/14985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=14985"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/14985\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=14985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=14985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=14985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}