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 6353139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:21:29+00:00 2026-05-24T22:21:29+00:00

I am making an ASP.Net MVC3 application. I use for now the built in

  • 0

I am making an ASP.Net MVC3 application. I use for now the built in Authentication code that comes with a Visual Studio 2010 project. The problem is dat I need to retrieve the logged in user’s database ID as soon as he has logged in. I do that now by adding code to the Login Action of the Account controller that retrieves the ID from the database by looking it up by username. This works for new logins, but not for “remembered” ones. On restarting the application the last user is automatically logged in again, but the Login code is not fired, so I do not get the database ID.

How can I solve this?

EDIT:
I tried to implement Daniel’s solutions which looks promising and I came up with this code. It nevers gets called though! Where have I gone wrong?

Global.asax.cs:

protected void Application_Start()
{
    Database.SetInitializer<StandInContext>(new StandInInitializer());

    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    this.AuthenticateRequest +=
       new EventHandler(MvcApplication_AuthenticateRequest);
}

void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
{
    if(Request.IsAuthenticated)
    {
        using (var db = new StandInContext())
        {
            var authenticatedUser = db.AuthenticatedUsers.SingleOrDefault(
                user => user.Username == User.Identity.Name);
            if (authenticatedUser == null)
                return;
            var person = db.Persons.Find(authenticatedUser.PersonID);
            if (person == null)
                return;

            Context.User = new CustomPrincipal(
                             User.Identity, new string[] { "user" })
                           {
                               Fullname = person.FullName,
                               PersonID = person.PersonID,
                           };
        }
    }
}
  • 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-24T22:21:31+00:00Added an answer on May 24, 2026 at 10:21 pm

    You can use the AuthenticateRequest event in your Global.asax.cs:

    protected void Application_AuthenticateRequest()
    {
        if (Request.IsAuthenticated)
        {
            // retrieve user from repository
            var user = _membershipService.GetUserByName(User.Identity.Name);
            // do other stuff
        }
    }
    

    Update:

    Now that I see what you’re trying to do a little clearer, I would recommend against using sessions in this particular case. One reason is that Session requires a reference to System.Web, which you don’t have access to from some places, like a business logic layer in a separate class library. IPrincipal, on the other hand, exists for this very reason.

    If you need to store more user information than what IPrincioal provides, you simply implement it and add your own properties to it. Easier yet, you can just derive from GenericPrincipal, which implements IPrincipal and adds some basic role checking functionality:

    CustomPrincipal.cs

    public class CustomPrincipal : GenericPrincipal
    {
        public CustomPrincipal(IIdentity identity, string[] roles)
            : base(identity, roles) { }
    
        public Guid Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email  { get; set; }
        ...
    }
    

    So then you replace the default principal with your own in AuthenticateRequest, as before:

    Global.asax.cs

    protected void Application_AuthenticateRequest()
    {
        if (Request.IsAuthenticated)
            Context.User = _securityService.GetCustomPrincipal(User.Identity.Name);
    }
    

    And that is it. The greatest advantage you get is that you automatically get access to your user data from literally everywhere, without having to stick a userId parameter into all your methods. All you need to do is cast the current principal back to CustomPrincipal, and access your data like so:

    From your razor views:

    <p>Hello, @((CustomPrincipal)User).FirstName!</p>
    

    From your controllers:

    var firstName = ((CustomPrincipal)User).FirstName;
    

    From a business logic layer in another assembly:

    var firstName = ((CustomPrincipal)Thread.CurrentPrincipal).FirstName;
    

    To keep things DRY, you could pack this into an extension method and hang it off IPrincipal, like so:

    public static class PrincipalExtensions
    {
        public static string GetFirstName(this IPrincipal principal)
        {
            var customPrincipal = principal as CustomPrincipal;
            return customPrincipal != null ? customPrincipal.FirstName : "";
        }
    }
    

    And then you would just do @User.GetFirstName(), var userName = User.GetFirstName(), Thread.CurrentPrincipal.GetFirstName(), etc.

    Hope this helps.

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

Sidebar

Related Questions

Error context: Visual Studio 2010 Service Pack 1 ASP.NET MVC 3 Application IE9 with
I'm making an ASP.Net MVC 3 application in VS 2010. I have a task
I am making application in ASP.Net 2.0 in VS 2010, i have created crystal
I have an ASP.NET MVC 3 application that uses the Ninject.MVC3 extension to setup
We use ASP.NET, C# When making an update to one of our websites, we
In an ASP.NET MVC application, I'm making logic for Admin to accept or reject
I am making my first web application with ASP.NET and I am having a
if i have a standard ASP.NET application, is there any difference between making an
I am using asp.net mvc3. I am making a bitmap using text by system.drawing.
When making calls to a webservice from an asp.net web application, to avoid creating

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.