Tag Archives: Azure

Using Postman to make changes to signed-in user’s profile

prerequisites:

Azure AD B2C has an app.
This app should have these minimum permissions which you need to set:

Set up Postman to query for MS Graph API

ref – https://docs.microsoft.com/en-us/graph/use-postman

Open up Postman app.

Choose File | Import ….

Select Import From Link.

Paste the following two URLs and choose Import after each:

https://raw.githubusercontent.com/microsoftgraph/microsoftgraph-postman-collections/master/Microsoft%20Graph.postman_collection.json
https://raw.githubusercontent.com/microsoftgraph/microsoftgraph-postman-collections/master/Microsoft%20Graph.postman_environment.json

You should now see the Microsoft Graph environment in the top right environment drop down by the eye icon. Now you need to set up your environment.

Set up application API calls

Choose the No environment drop down in top right corner.

Select Microsoft Graph environment.

Choose the eye icon to the right and then choose Edit.

Enter your Microsoft Identity Application in the current (not initial) variables:
ClientID,
ClientSecret
TenantID.

When you created your app, it will have its own clientID and tenantID. For ClientSecret, just generate one like so:

Make sure you copy the secret onto a text, because after refresh, Azure will cover it.

Select Update.

Close the Manage Environments dialog box. In the MicrosoftGraph | Application collection on left side, choose
Get App-only Access Token.

Then choose Send.

You’ll get a App Access Token.

On the left sidebar, under Application, click on Get users. In your environment variables, make sure you copy and paste this App Access Token into the AppAccessToken variable.

Confirm that it is set under Bearer Token in Authorization tab.

Now you are ready to make changes to the user profile data.

On the left side, under Application | Users folder and choose Get Users.

The URL should be https://graph.microsoft.com/v1.0/users/ the HTTP method should be GET.

Then choose Send.

You’ll get a list of the users. Choose one user, and copy the id.

Changing the profile data

Now, on the top, change the HTTP method to PATCH. copy and paste the id at the end like so:

https://graph.microsoft.com/v1.0/users/302cd19a-6bd6-4cb0-a161-00cf925d8da7

Then choose Body tab. Select the raw radio button. Finally, choose JSON (application/json) for the pull down, which was originally defaulted to text.

Let’s say we want to change property displayName. We use JSON for this:

click send.

GET POST using fetch from client to web api

Using fetch, we get a GET request on a web API using Azure authentication and passport

node server

client

Using fetch, we get a POST request on a web API using Azure authentication and passport

node server

client

Passing a form data to the Web API with no authentication

client

Create and Deploy a React App on MS Azure

ref –

  • https://css-tricks.com/deploying-a-client-side-rendered-create-react-app-to-microsoft-azure/
  • https://stackoverflow.com/questions/57618453/process-for-react-app-deployment-to-azure-web
  • download demo

First we search for “App Services”. Then click on the plus button to create one.

Since I”m on free trial, that is the account I belong to. We give it a name for the group that this app belongs to. Stack should be Node. Region should be East Asia.

Use default on the rest of the options. Review your changes and click create.

Once it finishes creating, you’ll come back to the dashboard with your newly created Web Service. Click on it and you’ll see the web service’s stats. Notice the URL. Click on it to see your default page. This page is actually serviced from your server’s site/wwwroot/hostingstart.html. You can verify this by clicking on the SSH icon on your left, a window pops up and you’re in your server. cd into site/wwwroot and you’ll be able to see it.

Creating React App locally

We create the app and name it azure-react-demo:

npx create-react-app azure-react-demo

We go into the directory and install react-router-dom for routing features:


cd azure-react-demo
npm i react-router-dom

In your directory, you should now see node_modules, public, src ..etc.

Then create pages folder in src.

src/App.js

src/pages/Home.js

src/pages/Page1.js

src/pages/Page2.js

src/App.css

run npm start and you should see a simple app with pages. We are going to deploy it to Azure.

Deploy to Azure

First go to Deployment Credentials

set up credentials for a user.

Now go to the deployment center and set it up for our local project.

After the third step, Azure generates a local git repo for you. And it gives you a remote link to point your react app to.

something like this: https://YourAppName.scm.azurewebsites.net/YourAppName.git

Create the build folder

Now in our root directory, npm run build

Once the build folder generates, we CD into it:

Make sure you use the git url like this: https://YourAppName.scm.azurewebsites.net/YourAppName.git
And then put in your username and password when it prompts you.

Startup command

If you are using Windows, you’re ready to go. Because we are using Node, we need to do something that would allow Azure to point to our static website.

Configuration > General Settings > Startup Command:

pm2 serve /home/site/wwwroot –no-daemon –spa

If you use react-router (which we are using) and wants to make any direct access on custom routes be handled by index.html you need to add –spa option on the same command.

then go to your site and it should work. When you click around, all the links should work. In addition, entering URLs in the browser will work also.

IF you do not put in the Startup command, the site will always display the default page. It will not even run your app.
If you put the startup command WITHOUT the –spa, the site will work, but you can’t access other pages through the URL. Every URL page must be accessed through the front page.

Thus, pm2 serve /home/site/wwwroot –no-daemon –spa solves both problems.

Refresh Token for Azure Active Directory

30 Days of Zumo.v2 (Azure Mobile Apps): Day 7 – Refresh Tokens

Getting the key from your Azure AD Portal

Go to your Azure Active Directory Portal via manage.windowsazure.com

Sign in, and click on Active Directory, then you should be able to see your app.

refresh_token_aad

Click on Applications, then your app name

refresh_token_applications

In the next screen, click on Configure, then scroll down the page

refresh_token_configure

Under ‘Keys”, select a year, and then save. You will then see the secret key appear. Copy that into test.js under the other global variables.

client_secret

Save your client ID, and your key because we will be using it later.

Configured the Azure AD service to use refresh tokens

Log into your portal, select your app, scroll down to Resource explorer. You’ll see the next blade have a Go link. Click on the go link.

refresh_token_portal

A separate page will appear with a lot of resource data. On the left hand side of the window is a tree explorer of your app’s resources. Expand the tree menu config and then authSettings.

Because this is a dangerous place, it’s set to read-only mode.

Click on the grey Read/Write box at the top of the screen, then click on Edit next to the PUT button.

I need to set two things. Firstly, the key that I created in the Azure AD portal needs to be copied into the clientSecret field as a string. Secondly, I need to set the additionalLoginParams to [“response_type=code id_token”], like this:

refresh_token_resource_explorer

Also make sure the client ID matches from your Active Directory management portal from above.

Deleting in Azure

Given that a refresh pull involves pulling data that is filtered according to an attribute, Soft Delete involves setting that attribute to YES/NO. This affects clients in that they will then not be able to pull that data. Additionally, that data is kept safe in Easy Table for future references and undeletes.

For example, let’s say you create an attribute “complete”.
When pulling data, you may specify that you want to pull all data that has NO for attribute “complete”.

Once you assign YES for attribute complete on say row 88, client refresh pulls will not include row 88 anymore. It will include all rows with NO for attribute “complete”.

When fetching from your fetch controller/core data, simply filter data according to complete == NO.

HARD DELETE – Delete on Local and Server

If you want to remove local AND server data, all you have to do is call the delete method from your MSSyncTable.

It sends a request to your local data source to remove the given item, then queues a request to send the delete to the mobile service.

It first removes the data locally.
Then, when the queued request goes through into the mobile service, then it will update remotely, and you can log into your Azure account, look at the Easy Tables, and see that the item has been removed.

Notes

Do not remove data by hand on the backend directly. Currently, MS have no way to re-syncing, and your client app will have many error messages on its request queue.