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
Developer technologies | ASP.NET | ASP.NET API
2 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
2025-05-07T18:27:41.67+00:00 -
Danny Nguyen (WICLOUD CORPORATION) 800 Reputation points Microsoft External Staff
2025-07-08T08:00:45.6233333+00:00 Hi,
To allow anonymous access to a specific ASPX page while using Azure AD for authentication, here are a few things you can try:
Web.config Modifications: You can modify your
web.config
file to allow anonymous access for that specific page. You’ll want to add a<location>
section that explicitly allows anonymous access:<location path="YourPage.aspx"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location>
Review Startup Configuration: Since you mentioned you are utilizing OWIN, make sure your middleware for JWT bearer authentication is set up correctly and that it does not block access to your specified page inadvertently.
Handling Token in HTTP Client: Ensure that the access token you are generating has the correct scopes and permissions if you're attempting any secured resources.
As for using a token without requiring a user to login, note that ASP.NET Web Forms typically rely on cookie-based authentication rather than bearer tokens. If possible, consider:
- Converting the ASPX page to an API endpoint – instead of returning an ASPX page, you could transform the logic to serve the data you need in a web API response. This will allow better integration with bearer tokens.
Implementing Claims-based Access: Within your application, consider checking the user's claims directly in the code behind of your ASPX page to control the content returned based on whether the user is authenticated or not.It looks like you're trying to expose a single ASPX page for anonymous access in your .NET 4.7.2 Web Forms app that's configured with Azure AD authentication. I understand you're currently hitting a login prompt when trying to access this page with a token, even after taking steps to implement bearer token support.
To allow anonymous access to a specific ASPX page while using Azure AD for authentication, here are a few things you can try:
Web.config Modifications: You can modify your
web.config
file to allow anonymous access for that specific page. You’ll want to add a<location>
section that explicitly allows anonymous access:<location path="YourPage.aspx"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location>
**Review Startup Configuration**: Since you mentioned you are utilizing OWIN, make sure your middleware for JWT bearer authentication is set up correctly and that it does not block access to your specified page inadvertently. **Handling Token in HTTP Client**: Ensure that the access token you are generating has the correct scopes and permissions if you're attempting any secured resources. As for using a token without requiring a user to login, note that ASP.NET Web Forms typically rely on cookie-based authentication rather than bearer tokens. If possible, consider: **Converting the ASPX page** to an API endpoint – instead of returning an ASPX page, you could transform the logic to serve the data you need in a web API response. This will allow better integration with bearer tokens. **Implementing Claims-based Access**: Within your application, consider checking the user's claims directly in the code behind of your ASPX page to control the content returned based on whether the user is authenticated or not.