How to create url-wise authentication for your web app

By
Published Jul 19 2021 03:23 AM 1,236 Views
Microsoft

In this post, I will introduce how to configure URL-wise authentication for your web app with a codeless approach.

 

Authentication is always an essential but error-prone part in any web application. In the Azure Web App, EasyAuth saves the user from handling authentication in the application code. EasyAuth can be enabled by a one-click (codeless) operation from the portal, and then the website will only accessible by a restricted group. When the easyAuth feature is enabled for the web app, the client will be required to login in with their credential when browsing the website. Below is a screenshot of login window with easyAuth configured.

 

Zehui_0-1626567496186.png

 

 

While easyAuth is useful in general, some user may want to have URL-wise authentication for their web app; in other words, exposing some subdirectory of their website to the public while requiring authentication for the remaining directory. For example, a company use exampleCompany.com to host their website, and they want to keep all the content of the website internal except for the subdirectory exampleCompany.com/guest, which is used by the guest visiting the company. In this case, we can use another built-in feature for Azure web app called "configuring  authentication using a file"

 

I will hereby provide  the step-by-step instructions to control web app “easy auth” per URL below. The link for the documentation is the following:

 

Advanced usage of AuthN/AuthZ - Azure App Service | Microsoft Docs

 

This feature is currently on preview, but it should be a stable feature which satisfies our requirement.

 

Step 1

 

Taking the example of "exampleCompany.com" earlier, the first step is to go to the Resource Explorer (azure.com) and find your site exampleCompany.

  1. Find your own site name. In your case, it should be “exampleCompany”
  2. Navigate to the authsettings blade, and you should see the relevant values on the right
  3. Change the required settings as shown in the following:     
  •    Set enabled to "true"
  •    Set isAuthFromFile to "true"
  •    Set authFilePath to the name of the file (for example, "auth.json")

 

Zehui_1-1626567496183.png

 

 

Step 2

Create a new registration for your web app in azure active directory. (You do not need to do this if you have an existing one for your web app “exampleCompany”)

  1. Go to the App registration blade
  2. Click new registration

 

 

Zehui_2-1626567496182.png

 

 

Step 3

Create a json file called auth.json under the /wwwroot folder in your web app. The minimal settings which satisfy your requirements should look like the following.

  1. Should be your tenant id in the azure active directory (AAD)
  2. Should be your client id in the AAD
  3. Should be the name of an application setting whose value is the client secrets of the AAD
  4. Should be url of your web app; in our case, it is http://exampleCompany,com

Zehui_3-1626567496179.png

 

If you have done these steps correctly, your web app should behave as your expectation. The incoming request to the https://exampleCompany.com/guest do not need authorization and requests to other sub directories will require authorization. Please note that the you should configure your AAD correctly such that it includes all the required information to authorize your app.

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2559287%22%20slang%3D%22en-US%22%3EHow%20to%20create%20url-wise%20authentication%20for%20your%20web%20app%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2559287%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20post%2C%20I%20will%20introduce%20how%20to%20configure%20URL-wise%20authentication%20for%20your%20web%20app%20with%20a%20codeless%20approach.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAuthentication%20is%20always%20an%20essential%20but%26nbsp%3Berror-prone%20part%20in%20any%20web%20application.%20In%20the%20Azure%20Web%20App%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapp-service%2Foverview-authentication-authorization%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EEasyAuth%3C%2FA%3E%20saves%20the%20user%20from%20handling%20authentication%20in%20the%20application%20code.%20EasyAuth%20can%20be%20enabled%20by%20a%20one-click%20(codeless)%20operation%20from%20the%20portal%2C%20and%20then%20the%20website%20will%20only%20accessible%20by%20a%20restricted%20group.%26nbsp%3BWhen%20the%20easyAuth%20feature%20is%20enabled%20for%20the%20web%20app%2C%20the%20client%20will%20be%20required%20to%20login%20in%20with%20their%20credential%20when%20browsing%20the%20website.%20Below%20is%20a%20screenshot%20of%20login%20window%20with%20easyAuth%20configured.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Zehui_0-1626567496186.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296598i6A89D29A1D5976DF%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Zehui_0-1626567496186.png%22%20alt%3D%22Zehui_0-1626567496186.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20easyAuth%20is%20useful%20in%20general%2C%20some%20user%20may%20want%20to%20have%20URL-wise%20authentication%20for%20their%20web%20app%3B%20in%20other%20words%2C%20exposing%20some%20subdirectory%20of%20their%20website%20to%20the%20public%20while%20requiring%20authentication%20for%20the%20remaining%20directory.%20For%20example%2C%20a%20company%20use%3CSTRONG%3E%20exampleCompany.com%3C%2FSTRONG%3E%20to%20host%20their%20website%2C%20and%20they%20want%20to%20keep%20all%20the%20content%20of%20the%20website%20internal%20except%20for%20the%20subdirectory%20%3CSTRONG%3EexampleCompany.com%2Fguest%3C%2FSTRONG%3E%2C%20which%20is%20used%20by%20the%20guest%20visiting%20the%20company.%20In%20this%20case%2C%20we%20can%20use%20another%20built-in%20feature%20for%20Azure%20web%20app%20called%20%22%3CSTRONG%3Econfiguring%26nbsp%3B%20authentication%20using%20a%20file%3C%2FSTRONG%3E%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20will%20hereby%20provide%26nbsp%3B%20the%20step-by-step%20instructions%20to%20control%20web%20app%20%E2%80%9Ceasy%20auth%E2%80%9D%20per%20URL%20below.%20The%20link%20for%20the%20documentation%20is%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fapp-service%2Fapp-service-authentication-how-to%23-configure-using-a-file-preview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAdvanced%20usage%20of%20AuthN%2FAuthZ%20-%20Azure%20App%20Service%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20feature%20is%20currently%20on%20preview%2C%20but%20it%20should%20be%20a%20stable%20feature%20which%20satisfies%20our%20requirement.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--408870158%22%20id%3D%22toc-hId--409558198%22%3EStep%201%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETaking%20the%20example%20of%20%22%3CSTRONG%3EexampleCompany.com%3C%2FSTRONG%3E%22%20earlier%2C%20the%20first%20step%20is%20to%20go%20to%20the%20%3CA%20href%3D%22https%3A%2F%2Fresources.azure.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EResource%20Explorer%20(azure.com)%3C%2FA%3E%20and%20find%20your%20site%20exampleCompany.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EFind%20your%20own%20site%20name.%20In%20your%20case%2C%20it%20should%20be%20%E2%80%9CexampleCompany%E2%80%9D%3C%2FLI%3E%0A%3CLI%3ENavigate%20to%20the%20%3CSTRONG%3Eauthsettings%3C%2FSTRONG%3E%20blade%2C%20and%20you%20should%20see%20the%20relevant%20values%20on%20the%20right%3C%2FLI%3E%0A%3CLI%3EChange%20the%20required%20settings%20as%20shown%20in%20the%20following%3A%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CUL%3E%0A%3CLI%3E%26nbsp%3B%20%26nbsp%3BSet%26nbsp%3Benabled%26nbsp%3Bto%20%22true%22%3C%2FLI%3E%0A%3CLI%3E%26nbsp%3B%20%26nbsp%3BSet%26nbsp%3BisAuthFromFile%26nbsp%3Bto%20%22true%22%3C%2FLI%3E%0A%3CLI%3E%26nbsp%3B%20%26nbsp%3BSet%26nbsp%3BauthFilePath%26nbsp%3Bto%20the%20name%20of%20the%20file%20(for%20example%2C%20%22auth.json%22)%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Zehui_1-1626567496183.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296597iD6FB2503C2C44EC1%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Zehui_1-1626567496183.png%22%20alt%3D%22Zehui_1-1626567496183.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2078642675%22%20id%3D%22toc-hId-2077954635%22%3EStep%202%3C%2FH2%3E%0A%3CP%3ECreate%20a%20new%20registration%20for%20your%20web%20app%20in%20azure%20active%20directory.%20(You%20do%20not%20need%20to%20do%20this%20if%20you%20have%20an%20existing%20one%20for%20your%20web%20app%20%E2%80%9CexampleCompany%E2%80%9D)%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EGo%20to%20the%20App%20registration%20blade%3C%2FLI%3E%0A%3CLI%3EClick%20new%20registration%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Zehui_2-1626567496182.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296596i4E8B30E2A85EDDF1%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Zehui_2-1626567496182.png%22%20alt%3D%22Zehui_2-1626567496182.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-271188212%22%20id%3D%22toc-hId-270500172%22%3EStep%203%3C%2FH2%3E%0A%3CP%3ECreate%20a%20json%20file%20called%20auth.json%20under%20the%20%2Fwwwroot%20folder%20in%20your%20web%20app.%20The%20minimal%20settings%20which%20satisfy%20your%20requirements%20should%20look%20like%20the%20following.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EShould%20be%20your%20tenant%20id%20in%20the%20azure%20active%20directory%20(AAD)%3C%2FLI%3E%0A%3CLI%3EShould%20be%20your%20client%20id%20in%20the%20AAD%3C%2FLI%3E%0A%3CLI%3EShould%20be%20the%20name%20of%20an%20application%20setting%20whose%20value%20is%20the%20client%20secrets%20of%20the%20AAD%3C%2FLI%3E%0A%3CLI%3EShould%20be%20url%20of%20your%20web%20app%3B%20in%20our%20case%2C%20it%20is%20%3CA%20href%3D%22http%3A%2F%2FexampleCompany%2Ccom%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttp%3A%2F%2FexampleCompany%2Ccom%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Zehui_3-1626567496179.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F296599iBDEEFC42497A3E2E%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Zehui_3-1626567496179.png%22%20alt%3D%22Zehui_3-1626567496179.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20have%20done%20these%20steps%20correctly%2C%20your%20web%20app%20should%20behave%20as%20your%20expectation.%20The%20incoming%20request%20to%20the%20%3CA%20href%3D%22https%3A%2F%2FexampleCompany.com%2Fguest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2FexampleCompany.com%2Fguest%3C%2FA%3E%20do%20not%20need%20authorization%20and%20requests%20to%20other%20sub%20directories%20will%20require%20authorization.%20Please%20note%20that%20the%20you%20should%20configure%20your%20AAD%20correctly%20such%20that%20it%20includes%20all%20the%20required%20information%20to%20authorize%20your%20app.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2559287%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20class%3D%22lia-link-navigation%20lia-js-predefined-label%22%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Fpostpage%2Fboard-id%2FAppsonAzureBlog%23%22%20name%3D%22Web%20Apps%22%20aria-label%3D%22Choose%20Label%3A%20Web%20Apps%22%20target%3D%22_blank%22%3EWeb%20Apps%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20class%3D%22lia-link-navigation%20lia-js-predefined-label%22%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Fpostpage%2Fboard-id%2FAppsonAzureBlog%23%22%20name%3D%22.NET%22%20aria-label%3D%22Choose%20Label%3A%20.NET%22%20target%3D%22_blank%22%3E%3C%2FA%3E%3CA%20class%3D%22lia-link-navigation%20lia-js-predefined-label%22%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Fpostpage%2Fboard-id%2FAppsonAzureBlog%23%22%20name%3D%22.NET%22%20aria-label%3D%22Choose%20Label%3A%20.NET%22%20target%3D%22_blank%22%3E.NET%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20id%3D%22link_17%22%20class%3D%22lia-link-navigation%20lia-js-predefined-label%22%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Fpostpage%2Fboard-id%2FAppsonAzureBlog%23%22%20name%3D%22Azure%20Active%20Directory%22%20aria-label%3D%22Choose%20Label%3A%20Azure%20Active%20Directory%22%20target%3D%22_blank%22%3EAzure%20Active%20Directory%3C%2FA%3E%3CA%20id%3D%22link_6%22%20class%3D%22lia-link-navigation%20lia-js-predefined-label%22%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Fpostpage%2Fboard-id%2FAppsonAzureBlog%23%22%20name%3D%22.NET%22%20aria-label%3D%22Choose%20Label%3A%20.NET%22%20target%3D%22_blank%22%3E%3C%2FA%3E%3CA%20id%3D%22link_0%22%20class%3D%22lia-link-navigation%20lia-js-predefined-label%22%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fforums%2Fpostpage%2Fboard-id%2FAppsonAzureBlog%23%22%20name%3D%22Web%20Apps%22%20aria-label%3D%22Choose%20Label%3A%20Web%20Apps%22%20target%3D%22_blank%22%3E%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2559287%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Active%20Directory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWeb%20Apps%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jul 19 2021 12:34 AM
Updated by: