Web Application Update: Adding identity information of the user who has uploaded a file to a Storage Account

As a follow up to my previous post:

Creating a web application with .NET Core for uploading files into an Azure Storage Account

One of the challenges I had while configuring the Logic App to process the files that were uploaded to the Storage account was that I am unable to determine the user who performed the upload. While there is the option to use the Log Analytics workspace logs, this wasn’t practical because there is a slight delay for when an upload that has taken place and when the activity logged in the StorageBlobLogs table.

Giving this a bit of thought, I had 2 ideas on how we can address this (not in order of preference):

  • Option #1 – Add 2 Metadata Key Value pairs to the blob that is uploaded 
  • Option #2 – Create a JSON file with key value pairs to identify the user

Let’s go through the options:

Option #1 – Add 2 Metadata Key Value pairs to the blob that is uploaded 

This method can be achieved by updating the web application’s Upload.cshtml.cs to capture and write 2 Metadata Key Value pairs for the blob that has been updated as shown here:

  1. userprincipalname
  2. objectid

**Note that metadata keys cannot have uppercase, which is why the leading characters are not capitalized.

Option #2 – Create a JSON file with key value pairs to identify the user

The second option is to write a JSON file that contains the same key value pair in the content for each file and place it into a subfolder in the container named logs:

Going through the thought process of what changes need to be made to the Logic App, I realized that there isn’t a built-in Action to extract custom metadata key value pairs. This leads me more towards writing a JSON log as it would be much easier to obtain the information than to call an Azure Function with, say, a Python script to retrieve the information.

Note that if we write a JSON file into the subfolder of the container storing the uploaded files, the event grid will trigger an event and the Logic App will try to process the JSON file. To prevent this from happening, we’ll need to add a filter in the event grid to ignore and not trigger on JSON file writes:

Key: subject
Operator: String does not begin with
Value: storageAccounts/test001/containers/uploads/blobs/logs/

**Note that blobs is inserted between the container and subfolder.

—————————————————————————————————————————————————————————————————

Having these 2 key value pairs will allow us to easily look up the uploader’s account properties (objectid), or simply include the UPN (userprincipalname) of the user in the email notification.

The Upload.cshtml.cs code in the following repo has been updated to provide the additional Option 1 and 2 functionality: https://github.com/terenceluk/AzureFileUpload-Dotnet-WebApp