Use an HTTP-only cookie to store the token after it is first received. This way, the token gets automatically sent with every request by the browser — including JS, CSS, and static resource fetches from DFM.
Pseudo Code (Logical Flow)
FUNCTION ServeDfMonStatics(request):
IF access_token IS IN QUERY PARAMS:
// first request after login, store token in cookie
SET COOKIE "access_token" with value from query
REDIRECT to same endpoint WITHOUT token in query (clean URL)
ELSE IF access_token IS IN COOKIE:
// All future requests will have token from cookie
PROCEED to serve DFM static content
ELSE:
// First time, no token yet — redirect to login
REDIRECT to Azure login page with proper redirect_uri
Actual Code (C# Azure Function)
[Function(nameof(MyCustomDfMonEndpoint))]
public async Task<HttpResponseData> ServeDfMonStatics(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = Globals.DfMonRoutePrefix + "/{p1?}/{p2?}/{p3?}")] HttpRequestData req,
string p1, string p2, string p3)
{
// Check for token in query parameter
string tokenFromQuery = req.Query["access_token"];
// Step 1: If token found in query, save it as cookie and redirect to clean URL
if (!string.IsNullOrEmpty(tokenFromQuery))
{
var response = req.CreateResponse(HttpStatusCode.Redirect);
response.Headers.Add("Set-Cookie", $"access_token={tokenFromQuery}; HttpOnly; Path=/; SameSite=Lax");
response.Headers.Add("Location", req.Url.AbsolutePath); // remove query from URL
return response;
}
// Step 2: Check if access token exists in cookies
string tokenFromCookie = null;
if (req.Headers.TryGetValues("Cookie", out var cookies))
{
var cookieStr = cookies.FirstOrDefault();
tokenFromCookie = cookieStr?
.Split(';')
.Select(c => c.Trim())
.FirstOrDefault(c => c.StartsWith("access_token="))?
.Split('=')[1];
}
if (!string.IsNullOrEmpty(tokenFromCookie))
{
// Token exists, proceed to serve DFM static resources
return await this.DfmServeStaticsFunction(req, p1, p2, p3);
}
// Step 3: If no token anywhere, redirect to login
var tenant = "your-tenant-id";
var clientId = "your-client-id";
var redirectUri = "https://yourfunctionhost/api/DFM"; // must match Azure AD redirect URI
var authUrl = $"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?" +
$"client_id={clientId}&response_type=code&redirect_uri={redirectUri}&response_mode=query&scope=openid%20profile%20email";
var loginResponse = req.CreateResponse(HttpStatusCode.Redirect);
loginResponse.Headers.Add("Location", authUrl);
return loginResponse;
}
Summary of What This Code Does
- First request with token (via
?access_token=xyz
) → Stores token in cookie → Redirects to clean URL - All future requests → Token is read from cookie, allowing continued access to DFM content
- If no token at all → Redirects user to Microsoft Login page
Hope this helps. Do let us know if you have any further queries.
If this answers your query, do click Accept Answer and Yes for "Was this answer helpful." And if you have any further questions, let us know.