Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 651167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:07:59+00:00 2026-05-13T22:07:59+00:00

I have a website that uses basic ASP.Net forms authentication. In the web.config file

  • 0

I have a website that uses basic ASP.Net forms authentication. In the web.config file we specify specific access rules for individual pages and directories. Everything works great.

However, now I have some new requirements. My domain contains many different sites setup among different subdomains. I have two DNS subdomains that map to this application. One is aaa.mysite.com and the other is http://www.mysite.com. If a particular web request is received from subdomain aaa.mysite.com for a page protected by FormsAuthentication, before the FormsAuthentication logic is handled (the user would be re-directed to the login page), I want to execute some code first. This code would essentially try to read a cookie from a third subdomain, say zzz.mysite.com, and if does not exist, Response.Redirect to a login page in the zzz.mysite.com application.

I tried handling this via a base class that any of my Forms Authentication protected pages could inherit from, and then calling the special code in the Page_PreInit function. However, FormsAuthentication handles the redirect to the Login page even before the PreInit function is called.

Does anyone know a good way to handle this case? If Page_PreInit won’t work, where can I put code so that it executes prior to the FormsAuthentication redirect does, but where I also have access to which page it is (and what class it inherits from, so I can see if it inherits from System.Web.UI.Page or if it inherits from my special BasePage).

Any ideas? I think I could use the Globals Application_BeginRequest, but then this would be called for every single request, which doesn’t seem like a very good idea.

I can’t be the first person who has needed a way to handle an event prior to the FormsAuthentication, so please if you could give me some additional ideas I would be very appreciative!

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:08:00+00:00Added an answer on May 13, 2026 at 10:08 pm

    If the cookie has been written to zzz.example.com then a site on www.example.com cannot read it – the way to share cookies across subdomains is to write them to .example.com.

    This can be configured in forms authentication using the domain attribute on the forms element in the web.config:

    <forms [...]
       domain=".example.com">
    

    Note the leading period in the domain names.

    Edit to respond to comment

    You should probably be hooking into the PostAuthenticateRequest event – this is fired after the users identity (or lack of) is established, and you could register a custom HttpModule to receive this event.

    Edit to show workings

    Ok, I’ve just tested the following setup:

    A web application project, with the following directory structure:

    /Default.aspx                    -- Simple aspx page.
    /Login.aspx                      -- Simple aspx page, with a Login control.
    /web.config                      -- Main application config.
    /Classes/CheckingAuthenticate.cs -- HttpModule, configured in root.
    /Restricted/Default.aspx         -- Simple asp page.
    /Restricted/web.config           -- Config file for authorization
    

    So, the root web.config sets up Forms authentication, using a standard ASP.NET membership provider, and sets /Login.aspx as the login page. I have also registered a custom HttpModule in there:

    <httpModules>
      <add name="CheckingAuthenticate" 
           type="TempWebApp.Classes.CheckingAuthenticate"/>
      [...]
    </httpModules>
    

    The web.config in /Restricted/ denies access to anonymous users (this could equally be done in a <location> element in the root):

    <configuration>
      <system.web>
        <authorization>
          <deny users="?"/>
        </authorization>
      </system.web>
    </configuration>
    

    I then have the following code in my http module:

    using System;
    using System.Web;
    using System.Web.Security;
    
    namespace TempWebApp.Classes {
      public class CheckingAuthenticate : IHttpModule {
    
        public void Dispose() {
            //clean-up code here.
        }
    
        public void Init(HttpApplication context) {
          context.PostAuthenticateRequest += OnPostAuthenticate;
        }
    
        public void OnPostAuthenticate(object sender, EventArgs e) {
          var app = sender as HttpApplication;
    
          if (!UrlAuthorizationModule.CheckUrlAccessForPrincipal(app.Request.Path,
                                                                app.User,
                                                                "GET")){
            //Code here to read cookies, redirect user etc.
          }
        }
      }
    }
    

    This will fire after the user has been authenticated, but before ASP.NET attempts to authorize the user, so you get a chance to check the access yourself and redirect instead. I’ve been able to hit breakpoints on that quite happily. I didn’t get to see the AuthorizeRequest or PostAuthorizeRequest events if the user didn’t have access to those pages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 360k
  • Answers 360k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ok Ive found a solution. Simply create a new repo… May 14, 2026 at 2:46 pm
  • Editorial Team
    Editorial Team added an answer It appears in Configure there is an Advanced option which… May 14, 2026 at 2:46 pm
  • Editorial Team
    Editorial Team added an answer You can't have final parameters in C# Methods are "final"… May 14, 2026 at 2:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.