typically webform pages do not support bearer tokens, as they use cookie authentication. you need to add bearer token support to your application.
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?
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
1 answer
Sort by: Most helpful
-
Bruce (SqlWork.com) 75,621 Reputation points Moderator
2025-05-07T18:27:41.67+00:00