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

  • SEARCH
  • Home
  • 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 7885101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:58:28+00:00 2026-06-03T04:58:28+00:00

Scenario I develop an MVC application that will be hosted in Windows Azure. During

  • 0

Scenario

I develop an MVC application that will be hosted in Windows Azure. During development, I test against a local database and local user membership service. In production, the application hits off a SQL Azure database and a cloud-hosted user membership service.

Sometimes I’ll log in to the local version of the site with a user account that only exists on my local machine. If I forget to log out but switch my machine out of test mode, things start to break.

The Problem

I set my hosts file to point my application URL at 127.255.0.0 so that browser requests are handled by the Azure emulator locally. This also means my test application and the production application use the same host name – cookies for one are seen as valid cookies for the other.

If I log in to my local system, an ASPXAUTH cookie is created for the domain and contains the credentials of the user from my local system. If I forget to log out and switch my hosts file back to normal (browser requests go to the live application), it sends the same ASPXAUTH cookie to the server.

Since this user doesn’t exist on the server, only locally, any requests like Membership.GetUser().Email return an object null exception.

What I’d Like to Do

Ideally, I could inject some code early in the request to check that the user exists. Something like:

MembershipUser user = Membership.GetUser();
if(user == null) {
    FormsAuthentication.SignOut();
}

This will automatically remove the ASPXAUTH cookie for invalid users. But where do I put it? I started looking at my controllers – all of them inherit from a BaseController class, but even putting this code in the base controller’s constructor isn’t early enough. I also looked at Global.asax, but I’m not sure which event would be best to tie this action to.

Also, I’m not even sure this is the right way to do things. If not, what is the best way to ensure that the authentication cookie is for a valid user before just depending on the membership class like I am currently?

  • 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-06-03T04:58:30+00:00Added an answer on June 3, 2026 at 4:58 am

    You may handle the below event in you global.aspx:

    public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
    { 
    }
    

    EDIT:

    In the above method, Membership.GetUser() will return null, since it takes the userName from HttpContext.Current.User.Identity.Name property and in this event the user has not yet been authenticated. The above method is invoked by FormsAuthenticationModule when it raised Authenticate even. You can handle this event like so:

    public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
    {
        if (FormsAuthentication.CookiesSupported &&
                Request.Cookies[FormsAuthentication.FormsCookieName] != null)
        {
            try
            {
                var formsAuthTicket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);
    
                MembershipUser user = Membership.GetUser(formsAuthTicket.Name);
                if (user == null)
                {
                    FormsAuthentication.SignOut();
                    Request.Cookies[FormsAuthentication.FormsCookieName].Value = null;
                }
            }
            catch (Exception ex)
            {
                //TODO:log ex
            }
        }
    }
    

    You can also consider to handle the AuthenticateRequest event by declaring the below method in the global.aspx.

    NOTE: Application_AuthenticateRequest event is fired after the FormsAuthentication_OnAuthenticate event.

    void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
        {                
            MembershipUser user = Membership.GetUser();
            if (user == null)
            {
                FormsAuthentication.SignOut();
                HttpContext.Current.User = null;
            }
        }            
    }
    

    Hope this helps..

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

Sidebar

Related Questions

We are looking to develop an application that will get data from a web
I Develop a one application which contaion the scenario given below in that Some
I have the following scenario that i will start to develop very soon, but
Scenario: I have a console application that needs to access a network share with
Scenario : There is this online questionaire that will be filled in by various
I am trying to develop a Windows Mobile 6 (in WF/C#) application. There is
This is my scenario: Our big customer demands that we should develop a part
I'm planning to develop a tablet based web application that is compatible on all
I've got a client who wants an ASP.NET MVC application. I'll develop it with
Here's the scenario: A C# Windows Application project stored in SVN is used to

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.