401 Unauthorized Error–Azure Active Directory (AD)

Here I am listing down all the possible solutions for 401 Unauthorized Error during AAD configuration for your App Service or Resources.

image

The scenario can be like this:

  • You are able to Authenticate & Get a Valid Token
  • While presenting the Token for service access the Error 401 Unauthorized is happening

Solution 1: Ensure Resource Parameter

Ensure that while requesting the Authentication Token you are specifying the resource parameter.  Without the parameter you will still get the token but the 401 Error will occur as there is no valid resource.

image

Solution 2: Ensure App ID URI mentioned in the Service

Go to App Services > Authentication blade > Advanced Settings

image

Solution 3: Delete & Create new App Registration

Some of the configuration errors will be tricky to find – especially in restricted access production environments.  So you can try following:

  • Delete the App Registration
  • Create new App Registration
  • Set the new App Registration parameters in App Service

Solution 4: View Application Logs

Go to Log Streaming to view the Application Logs.

image_thumb[10]

You can search on the Detailed error code (401.83) OR IDX10214 error code for more details.

Solution 5: View Kudu Logs

You can see the Authentication Token is passed or not through the Kudu > Debug Console > CMD > W3SVC Logs.

image

Summary

In this post we have seen how to troubleshoot AAD Authorization Error.  If you find another solution not listed here, please comment below so I can update back in the article.  Help us to Help you!

References

Testing with Postman

Advertisements

Testing with Postman

Now we can test the previous service with Postman.

Postman

Postman is a GUI Tool for testing HTTP APIs. It is much popular today that almost all Developers have it installed it in their machine.

You can download latest version of Postman from here.

https://www.getpostman.com/tools

Steps

Following are the steps to use Postman.

Get the Token

Create a new GET REQUEST as shown below.

image

Enter the following Values.

· Auth URL use the URL https://login.microsoftonline.com/Your-AAD-Tenant-Name-OR-Guid/oauth2/token and replace the highlighted area

· Client ID You can get this from Azure > App Service > Properties

· Client Secret You can get this from Client Credentials blad

· Grant Type Hardcode as client_credentials

· Resource You can get this from Service > Expose an API blade

image

Call the Service

Now call the service & If everything went fine you will see the response as below.

image

Now follow these steps:

· Create a new GET request

· Add the highlighted headers below

· Set the Token copied from previous step with the Bearer prefix on it

· Click the SEND button in Postman

On successful execution, you will get the results as shown above.

Summary

In this post you have seen how to call an AAD protected API Service with Postman. In real world scenarios we can combine the Token generation & attaching to Service request as a single step.

References

Postman – Automatically attach Token

Cosmos DB Advantages

Cosmos DB is gaining Traction exponentially & I would like to list the advantages of Cosmos DB here.

Globally Distributed means scalable across all countries & regions.

Low Latency is the core advantage of Cosmos DB.  This is achieved through SSD based storage, planet scale flexibility providing Millisecond response time.

Flexible Consistency offered through Varying Consistency Models like Strong, Boundless, Session, Prefix, Eventual.

Flexible Pricing Model allows metering of storage & throughput independently rather than CPU/Memory/IOPs.

Elastic Scalability will scale the database based on the request volumes with No Limits. aka Horizontal Scalability

Multi-Storage Model allows storage of data in form of key-value pairs, documents, graphs & columnar

Multi-API Model allows querying the database in SQL for document, MongoDB for document, Azure Table Storage for key-value, Cassandra for columnar etc.

High Security  achieved through End-to-end Encryption on data-in-transit & data-at-rest.

Application Insights in Azure

Application Insights is a Application Performance Management (APM) Service available for Developers & Administrators.  We can use it for web applications deployed to Azure.

Application Insights provides the following features:

  • Application Availability
  • Performance Monitoring
  • Usage Insights

Application Insights also provides Visual Studio Integration, Support for Windows Phone Store Apps etc.

Creation of Application Insights

Open Azure Portal > Create New Resource > Search Box > Enter Application Insights

image

Select the first option & Choose Create.

image

Enter Information in the appearing dialog.

image

Click the Create button in the bottom.

Wait a few minutes & your Application Insights will be created.

image

Now you are ready with the Application Insights panel as shown below.

image

Cosmos DB

In this article we can explore about Cosmos Db database & its usage.

Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model, planet-scale, low-latency, high-availability, No-SQL database stored as JSON format.

image

In the olden days Cosmos DB was called as Azure DocumentDB.

Planet Scale

Azure Cosmos DB will be replicated based on Geography region & hence low-latency. There will be Primary Write-database in one central location & Multiple Secondary Read-databases in multiple locations.

Due to the multiple geographical location availability low-latency & high-availability is possible.

Multi-API Support

Cosmos DB supports Multiple-API support:

· SQL API

· Mongo API

· Graph API

· Table API

· Cassandra API

JSON Format

JSON Format is used to store the data in Cosmos DB.

Consistency

Cosmos DB have 5 consistency levels to ensure consistency, availability and latency trade-offs.

Create Cosmos DB

We can create Cosmos DB from Azure Portal as shown below.

Open Azure Portal > Create new resource > Cosmos DB

image

image

Click the Ok button & Wait for few minutes for the Creation to be completed.

You will get the following screen after creation.

image

Start by Add Collection. Then Add a New Document.

image

You can add new properties in the document. The data is stored as JSON file. Ensure the ID is unique.

image

Now we can query the database using SQL. Choose the New SQL Query option > Enter SELECT statement & Execute the Query. You will get the following results as displayed.

image

Summary

In this article we have explored about Cosmos Db database & its usage.

My DAX Days…

In this post I would like to Record my DAX Learnings during my SQL Server Analysis Services days.

SQL Server Analysis Services

It is 1000X faster than SQL Server since the Data is stored in Memory in Compressed Format.

DAX

DAX stands for Data Analysis Expressions.  It is widely used in Analysis Services, Power BI etc. 

DAX looks similar to Excel Formulas.

DAX Measure

DAX Measure is the actual Formula.

DAX Variables

DAX Variables will be executed once they are Invoked

EVALUATE

Evaluates the Expression.

Eg: EVALUATE ( ‘DimProducts’)

Eg: EVALUATE( ‘DimProducts’ ) ORDER BY ‘DimProducts'[Date] DESC  –Sorting Example

For Scalar Values, Use EVALUATE (ROW(“Column”, Variable)) syntax

Note: Add an EVALUATE in front of the Expression

ROW

Returns a Table with a Single Row.

Eg: EVALUATE ROW(“Total Sales”, SUM(FactSales[Price]), 
         “Total Quantity”, SUM(FactSales[Quantity]))

image

SUMMARY

Returns a summary table.

EVALUATE
SUMMARIZE(
        DimProducts 
      , DimProducts[Id] 
      , DimProducts[Class] 
      , DimProducts[SubClass] 
      , “Sales Total”, SUM(FactSales[Price]) 
      , “Quantity Total”,  SUM(FactSales[Quantity]) 
      ) 

image

CALCULATE

Evaluates an Expression when modified by a Filter.

FILTER

Returns a Subset of Table or Expression.

Eg: EVALUATE
FILTER(DimProducts,DimProducts[Class]=”Home Theatre”)

image

*Note the double quotes

More Filters: https://bit.ly/2L3iOdr

SUM

Sum is an Aggregator.

Eg: EVALUATE
SUMMARIZE (
DimProducts,
“Sum”, SUM ( DimProducts[Price] )
)

image

SUMX

SumX is an Iterator.  Can Add Multiplications into this.

Eg: EVALUATE
SUMMARIZE (
DimProducts,
“Sum”, SUMX (DimProducts, DimProducts[Price] * 100)
)

image

ADDCOLUMNS

Adds calculated columns to the given table

References

https://www.sqlbi.com/

https://msdn.microsoft.com/en-us/query-bi/dax