<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikhil Shinde on Medium]]></title>
        <description><![CDATA[Stories by Nikhil Shinde on Medium]]></description>
        <link>https://medium.com/@nikhilshinde57?source=rss-d68bbd2d99ef------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*FjXneD27Vgxfzf3y.</url>
            <title>Stories by Nikhil Shinde on Medium</title>
            <link>https://medium.com/@nikhilshinde57?source=rss-d68bbd2d99ef------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 01:59:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nikhilshinde57/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Retrieving Azure AD Bearer Access Token to Access Azure App Services]]></title>
            <link>https://medium.com/@nikhilshinde57/retrieving-azure-ad-bearer-access-token-to-access-azure-app-services-5926f7f7ac99?source=rss-d68bbd2d99ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/5926f7f7ac99</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[azure-ad]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[azure-services]]></category>
            <dc:creator><![CDATA[Nikhil Shinde]]></dc:creator>
            <pubDate>Tue, 30 Oct 2018 05:00:01 GMT</pubDate>
            <atom:updated>2018-10-30T05:00:01.874Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KYxJ5-EWcJxBMQCrehOFeA.jpeg" /></figure><p>Hello Everyone. Today we are going to see how to retrieve <strong>Azure Active Directory Bearer</strong> <strong>Access Token</strong> to access web API’s or web app hosted on Azure and secured by authentication type as <strong>Log in with azure active directory</strong>.</p><p>Let’s imagine one scenario where we have developed one NodeJS web application and deployed it on Azure and we enabled the app service authentication for that hosted application. That web app has some REST API’s in backend and you want to give call to those REST API’s from your external application. I am assuaging you already have your web app hosted on Azure and you enabled above setting.</p><p>Let’s see where we can see that setting. Let’s log in to the Azure Portal with your credentials.</p><p>After that click on App services option located on left-hand side of azure portal main page. Once you click on that it will show you the all app services (Your applications list). Click on your specific application. It will show you a view like shown below. Click on <strong>Authentication/ Authorization</strong> option. It will show you the view where you have already enabled App Service Authentication and rest of the setting as shown in the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*cM3kKRqxU3961cFFYTA6dQ.png" /><figcaption>Azure Authentication / Authorization settings for web app</figcaption></figure><p>You will see some labels highlighted in yellow in above image. Let’s talk about what does that labels mean. <strong>App Service Authentication</strong> is use to secure your app. Just below that we have one option <strong>Action to take when request is not authenticated</strong>. Using that option, we can set what to do if the request is not authenticated. We have different option that we can set those options are.</p><p>· Allow anonymous request no action</p><p>· Log in with Azure Active Directory</p><p>· Log in with Facebook</p><p>· Log in with Google</p><p>· Log in with Microsoft account</p><p>· Log in with Twitter</p><p>So, if you set first option that means any one can access your application. If you apply rest of the any option that makes your application secured and only identified person can only access your application only after successfully login into Azure using any one of the authentication providers. We have different Authorization Providers as listed above.</p><p>In this article we are exploring second option that is <strong>Log in with Azure Active directory</strong>. If you are not aware about what is <strong>Azure active directory</strong> then you can read about this <a href="https://docs.microsoft.com/en-us/azure/active-directory/fundamentals/active-directory-whatis">here</a>. So, we have selected <strong>Log in with Azure Active directory</strong> option. After that we need to configure the authentication provider. Steps to do that as listed below.</p><ol><li>Select <strong>Azure Active Directory</strong>, and then select <strong>Express</strong> under <strong>Management Mode</strong>.</li></ol><p>2. Select <strong>OK</strong> to register the App Service app in Azure Active Directory. This creates a new app registration. If you want to choose an existing app registration instead, click <strong>Select an existing app</strong> and then search for the name of a previously created app registration within your tenant. Click the app registration to select it and click <strong>OK</strong>. Then click <strong>OK</strong> on the Azure Active Directory settings view. By default, App Service provides authentication but does not restrict authorized access to your site content and APIs. You must authorize users in your app code.</p><p>After completing above configuration click on save button. Now open google chrome or any browser in incognito mode and hit your application URL, it will redirect you to <strong>Azure AD login page</strong>.</p><p>Same with <strong>REST API’s</strong>, open <strong>Postman</strong> or give call to your rest API from <strong>code</strong> you will receive response<strong> 404 Unauthorized</strong>.</p><p>In this article we are dealing with the requirement in which I have my <strong>REST API’s hosted on Azure</strong> and I have applied the <strong>Log in with Azure Active Directory Authentication</strong>. I have another application in that I need to call those REST API’s. To access that API’s, I need a <strong>Bearer Access Token</strong> which will authenticate me on my <strong>Azure Active Directory</strong> and once I authenticated, I can access my API. If you are accessing that web application from browser that time if you are not logged in to you azure active directory it will <strong>Redirect to Azure Active Directory Login</strong> page. Enter your user name and password once you successfully log in you will get redirected to your application page. But in case of REST API’s we need to retrieve Bearer Access Token from azure and then we use that Bearer Access Token for accessing or giving call to REST API’s.</p><p>Let’s see how to retrieve Bearer Access Token from Azure AD. I have written the following function to <strong>Retrieve Bearer Access Token</strong> from Azure AD in NodeJS.</p><pre>let getToken = () =&gt;{</pre><pre>return new Promise((resolve,reject)=&gt;{<br>  <br>  let options = {             uri:`https://login.microsoftonline.com/${config.AzureADId}/oauth2/token`,<br>headers: {<br>     &#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39;<br>      },<br>body: `grant_type=password&amp;<br>       username=${global.config.Environment.UserName}&amp;<br>       password=${global.config.Environment.Password}&amp;<br>       client_id=${config.Environment.ClientId}&amp;<br>       resource=${config.Environment.Resource}&amp;<br>       redirect_uri=${config.Environment.RedirectUri}&amp;<br>       client_secret=${config.Environment.ClientSecret}`<br>   };</pre><pre>   request.post(options,(err,res,body)=&gt;{<br>       if(err || res.statusCode != 200){<br>            reject(err);<br>       }<br>       else<br>           resolve(JSON.parse(body).access_token);<br>    });<br>  });<br>}</pre><p>If you observe the above code you will see some JSON object named as option and in that object you will see some properties like username, password, client_id, resource, redirect_url and client_secret etc. User name and password, is your Azure AD user name and password that you have with you. When you create web app on Azure. Azure will create one application Id for that web app which is always 32-bit GUID. Let’s see where I can get that all information in Azure. I am assuming you already logged in into Azure portal. Now select <strong>App Services</strong> as shown in below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/266/1*OP_trl3U7TQSgvVFH37E_Q.png" /></figure><p>Now it will show you the <strong>list of App Services. </strong>Out of that select your app service /web app. That will show you the detail information of your app service. Next click on <strong>Authentication/ Authorization</strong> option located in setting section in left hand side. After that click on <strong>Azure Active Directory</strong> once you click on that it will open <strong>Azure Active Directory Settings</strong> view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*XcROLODxcoc2OYGQjAikWg.png" /></figure><p>On that view we have already configured the management mode in our previous steps. Just at the bottom of that view you will see one option named as <strong>Manage application</strong> click on that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*8nnCWGLQN1vwTiWxq1xGfQ.png" /></figure><p>It will open new view saying <strong>Registered app</strong> and where you can see your application name. In that view you will see setting option just click on that. It will open setting page for your registered application and finally we have reached on our targeted window where we will get all the required keys and ID’s needed for retrieving Bearer Access Token from NodeJS code. The page will look like as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*ph0sy8DKpLOMU0XvyMSvjQ.png" /></figure><p>Click on <strong>Properties</strong> where you can find one label named as <strong>Application ID</strong> that is your <strong>client_id</strong> and <strong>resource</strong>. Next click on <strong>Reply URLs</strong> option where you will get the <strong>redirect_url</strong>. Now very important step where we are going to generate <strong>Secret Key</strong> for your web application. Click on Keys option it will open Keys view as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*OSHKUzRtmr6JTahkK5vziQ.png" /></figure><p>As shown in above image enter key description and select expiry duration. You can generate key for 1, 2 years and there is option like never expires. Just for demo purpose I am selecting never expires option. Now click on save button located at the top of the page. Once you save that you will see the key in <strong>VALUE</strong> text box against your key description. Copy that and save somewhere on your machine note that the key will be visible once you close this window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*kctUTczZIi2JHfe3lu66sA.png" /></figure><p>Now we are pending with only one thing and that one is your <strong>Tenant Id</strong> or <strong>Azure Active Directory ID</strong>. When you create azure account for your organization that is nothing, but you have created the azure tenant for your organization. Let’s see where that <strong>Tenant ID</strong> is located. Go to main page of azure portal. On your left-hand side there is a list of option and in that list, you will see <strong>Azure Active Directory</strong> option click on that. It will show you one view on that new view on your left-hand side click on <strong>Properties</strong> option once you click on that it will show you a view named as <strong>Directory Properties</strong>. On that view you can find one label named as <strong>Directory ID</strong> that is nothing but your <strong>Azure Active Directory Id</strong>, <strong>Tenant Id</strong> or you can say <strong>Organization Id</strong>. Copy that and use it for variable named as AzureADId in our NodeJS code.</p><p>I have created one <strong>config file</strong> and that config file holds all above <strong>keys and secret keys</strong> as shown in below. At the time of function calling just import that config file and use it.</p><pre>let config = { <br>   Environment:{<br>      UserName = &quot;admin@mycompanydomain.com&quot;,<br>      Password = &quot;******&quot;,<br>      ClientId=&quot;********-a24f-44da-a6f7-73ec940b2ed4&quot;,<br>      ClientSecret=&quot;KrAys********1kysgU//eGU/L8tcLaP3jcQw/p0q7k=&quot;,<br> RedirectUri=&quot;https://testapp.azurewebsites.net/.auth/login/aad/callback&quot;,<br>      Resource=&quot;********-a24f-44da-a6f7-73ec940b2ed4&quot;<br>  }<br>};</pre><p>Now run that function, it will give you the <strong>Bearer Access Token</strong> in response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*5ox9DWdFopm_jdToqkPXfA.png" /></figure><p>Now you can use that <strong>Bearer access token </strong>to call/access your REST APIs. You need to pass that token in header by setting one property named as <strong>Authorization. </strong>Your code will look like that as shown in below.</p><pre>let getAllCustomers=(token)=&gt;{</pre><pre>return new Promise((resolve,reject)=&gt;{<br> <br>let options = {<br>      uri:&quot;https://testapp.azurewebsites.net/api/customers&quot;,<br>      headers:{<br>              &quot;Authorization&quot;: &quot;Bearer &quot; + token,<br>              &quot;accept&quot;: &quot;application/json;odata=verbose&quot;<br>            }<br>     };</pre><pre>request.get(options,(err,data,body)=&gt;{<br>        if(err)<br>           reject(err);<br>        else<br>           resolve(body);<br>       });<br>  });<br>}</pre><p>The above function will return all customers from my REST API as we are giving get request call on my Customers resource. This function takes <strong>Bearer Token as input</strong>. At the time of header setting we are setting one property named as <strong>Authorization</strong> as shown in above code. So, when your request goes to REST API hosted on Azure and enabled with Azure AD authentication that time it will check the header of your request. If it finds Authorization property with Bearer Access Token, then Azure will validate that token if that token is valid it will process your request further that means you will get response from your rest API end point else it will send you the 404 Unauthorized response. In this way we can secure our REST API’s or web app by setting Log in with Azure Active Directory authentication option and to access that secured API/app we can retrieve/generate Bearer Access Token and access our API securely. Hope this information is useful for you guys.</p><p>Happy coding😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5926f7f7ac99" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Issues with deploying Node.JS application on Azure]]></title>
            <link>https://medium.com/@nikhilshinde57/issues-with-deploying-nodejs-application-on-azure-383e3b38e1b4?source=rss-d68bbd2d99ef------2</link>
            <guid isPermaLink="false">https://medium.com/p/383e3b38e1b4</guid>
            <category><![CDATA[issues]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[deployment]]></category>
            <dc:creator><![CDATA[Nikhil Shinde]]></dc:creator>
            <pubDate>Tue, 28 Aug 2018 15:16:11 GMT</pubDate>
            <atom:updated>2018-09-02T13:49:29.193Z</atom:updated>
            <content:encoded><![CDATA[<p>Few days ago, I have completed one Node.JS application and to host that application on cloud I have selected Azure cloud. As first time am deploying Node.JS application on Azure I searched some information on google and saw some videos on YouTube. After reading blogs and watching videos I thought it is very easy and it’s just a few hours work including testing and bug fixes.</p><p>But, when I started the deployment, I deployed the code, but my app server failed to start. And then this journey started what goes wrong.</p><p>I will brief you guys before start discussing this issue. The application is simple I have created the web portal. The backend is fully written in Node.JS and ExpressJS. The frontend is fully written in AngularJS, Bootstrap and HTML 5. Both backend and frontend JavaScript code is written in ES6. So, as you know ES6 features like classes, const, let keywords everything I have used. On my local machine I have node version 8.9.3 was installed. Let’s start the application deployment on Azure.</p><p><strong>Deploy Node.JS Application On Azure:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x3b5BZ12WWgYgdAmvQuAPA.png" /></figure><p><strong>Deployment perquisite:</strong></p><p>1. Crate one empty Node.JS web app on Azure. You can find the steps for that <a href="https://azure.microsoft.com/en-in/resources/videos/create-a-web-app-with-nodejs-and-azure-app-service/">here</a>.</p><p>2. I use Visual Studio Code editor for the development so if you want to deploy the code directly from your visual studio code then you need to add <strong>Azure App Service</strong> extension to your visual studio code editor. How to add the extension that information you can find <a href="https://code.visualstudio.com/tutorials/app-service-extension/getting-started">here</a>.</p><p>3. Once you add extension then login into your azure account through that extension, that information is also available in above link.</p><p><strong>Deployment Steps:</strong></p><ol><li>When you successfully logged into your azure account, you will see the all subscription to which you have access and all web app which belongs to that subscription. As shown in below screen shot.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*YIr_pCn2yUFX6VJwwi-E4w.png" /></figure><p>Like as shown in above image I have access to say “XYZ” azure subscription and under that I have my empty Node.JS web app with name “todogo”.</p><p>2. Now as we have already created the empty Node.JS web app “todogo” that you can see under your subscription in visual studio shown in above screen shot.</p><p>3. Here I am assuming you have already opened the code in visual studio code if not please open it.</p><p>4. Now just right click on our empty Node.JS web app.</p><p>5. Now you can see there is one option named as “Deploy to web app…”</p><p>6. Just click that option and that’s it, the deployment process started.</p><p>7. The progress of the deployment you can see under output window, that you can open using short cut key Ctrl+Shift+U.</p><p>8. It will take few minutes depend upon the project size.</p><p>9. Once deployment complete you will see one deployment successful message in output window.</p><p>10. Now just browse your application by hitting your application URL in web browser.</p><p>Major issues that everyone will face, and my experience was those issues are very small one but without resolving those issues your application will not work. I have listed some of them which I have experienced.</p><p><strong>1. Unable to find main file:</strong></p><p>As we know when we initiate the node project by giving npm init cmd that time we give the name of the main file. That file contains your application start code.</p><p>Most of the time at root directory level we find that file and name of that file you can find in package.json file. In my case its name is server.js as shown in below screen shot highlighted in yellow color.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*3NFDrSY4QQlqnz6X9VxtyQ.png" /></figure><p>Azure will not understand this. We need to explicitly tell azure about this. To solve this issue, we need to add the web.config file and need to upload that on azure.</p><p>Now create web.config file at root directory and add below lines of code into that file and upload that file on azure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*FYaslhV24dOXo1N48XFSEQ.png" /></figure><p>These lines of code defines that the server.js is the main starting file for this Node.JS application. If your main file is under any sub directory like server/server.js then in that case, you need to specify the file name with file path like server/server.js.</p><p><strong>2. Unable to find/understand strict, const let keyword:</strong></p><p>By reading the error we can understand that Node.JS failed interpret that keywords. So, if you have written your Node.JS code in ES6 that time make sure that your node version should be &gt;= to Node.js v4.3.2. If azure is using node version below that then it will not understand the ES6 syntax or keywords.</p><p><strong>· Now how to set Node.JS version for my Azure Node.JS web app?</strong></p><p>There are different ways to set that Set in package.json file as shown below:</p><p><strong>{“engines”:{ “node” : “&gt;=4.3.2” } }</strong></p><p>Add above code in your package.json file and redeploy your code.</p><p><strong>· Set Node version for my Azure Node.JS web app from Azure Portal:</strong></p><p>On Azure portal open your web app. After that in left hand side you will find one option named as “Application settings”. Click on that it will show you the application settings option as shown in below screen shot.</p><p>Now on your left-hand side you will see the Application settings section and just in that section click on “Add new setting” option highlighted in yellow in below screenshot.</p><p>Now Add the key named as “WEBSITE_NODE_DEFAULT_VERSION” and set value as “8.9.3”. In my case I am using node version “8.9.3” so whichever node version is suitable for you, you can set that.</p><p>Once you set that save the current changes by clicking save button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*JxH-9S82_nZBIDaDqDpjjA.png" /></figure><p><strong>3. Update iisnode.yml file:</strong></p><p>This solution was worked for me. Now in that file you will find the Node.JS version set as 4.0.0. This is the default version azure set at the time of web app creation. No as shown in below image change the version and save the file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*XrfmDfdby1zGQgHBFmbObg.png" /></figure><p>In my case I am using node version 8.9.3 so I set that node version.</p><p>I will suggest setting node version don’t go for first 2 solution because my experience was I tried first 2 solution, but they failed but when I updated the iisnode.yml file that worked for me.</p><p><strong>4. Static file not found server return 404:</strong></p><p>In front-end html page we always give the relative path for css, js files. So, if you are using ExpressJs web framework then we do the following setting.</p><p><strong>app.use(express.static(path.join(__dirname, ‘public’)));</strong></p><p>By doing this when server receives a request for static file it will redirect or look for that file in public folder.</p><p>But this will not work there are some more setting we need to do for this. So, to tell our web app about this setting we need to add some code in web.config file as shown in below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*zcI5v0UxPpqHy6dmizbxCg.png" /></figure><p>Thank You :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=383e3b38e1b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>