Is there a way to expose a single aspx page for anonymous access in a .NET 4.7.2 ASP.NET Web Forms running with Azure AD Authentication?

Ciaccio, Ed 0 Reputation points
2025-05-07T15:19:34.2266667+00:00

We have a .NET 4.7.2 ASP.NET Web Forms application deployed under IIS running with Azure Active Directory Authentication.

The web app is registered as an application on Azure AD and requires users to login before accessing the site.

Is there a way to expose a single aspx page for anonymous access?

Or perhaps access the page via a token acquired from Azure, without requiring the user to login?

We are trying to return the aspx page as a byte array.

Prior to Azure AD, WebClient was used to access the page, passing in a username and password.

Below please find some sample code:


System.Net.WebClient webClient = new System.Net.WebClient();

Uri requestUri = new Uri(m_url);

System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(SiteSettings.AlerterUserName, SiteSettings.AlerterPassword, SiteSettings.AlerterDomain);

System.Net.CredentialCache credentialCache = new System.Net.CredentialCache();

credentialCache.Add(new Uri(SiteSettings.SiteUrl), "NTLM", credentials);

webClient.Credentials = credentialCache;

byte[] raw = webClient.DownloadData(requestUri);

m_body += System.Text.Encoding.Default.GetString(raw);


This is no longer valid with Azure.

We have replaced the use of WebClient with HttpClient, however instead of the requested page being returned, the site is requiring a login:


string token = GetAccessToken();

using (HttpClient httpClient = new HttpClient())

{

httpClient.BaseAddress = new Uri(m_url);

httpClient.DefaultRequestHeaders.Remove("Authorization");

httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

byte[] raw = httpClient.GetByteArrayAsync(m_url).GetAwaiter().GetResult();

m_body += System.Text.Encoding.Default.GetString(raw);

}


The following is returned:

<!-- Copyright (C) Microsoft Corporation. All rights reserved. -->

<!DOCTYPE html>

<html dir="ltr" class="" lang="en">

<head>

<title>Sign in to your account</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes">

...

Thank you,

Ed

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
426 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 75,621 Reputation points Moderator
    2025-05-07T18:27:41.67+00:00

    typically webform pages do not support bearer tokens, as they use cookie authentication. you need to add bearer token support to your application.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.