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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:10:59+00:00 2026-05-25T23:10:59+00:00

I just started an MVC 3 project using Forms Authentication. I have a web

  • 0

I just started an MVC 3 project using Forms Authentication. I have a web project (mvc project), and a data project. The data project handles all database interactions.

I need to store the current user id in my database on every insert and update.

Here’s what I am doing right now. I feel like this has to be a common scenario. Can anyone shed some light on a better way to do this?

  1. On LogIn or Register, I grab my current user record (by username) from the database and store the user id in session.
  2. When saving a record within my data project, I refer to the httpcontext (if it exists) and use the user id that is stored in session.

This works for all but one scenario. It fails when a user has a valid cookie saved, and so they are allowed to bypass the login/register action. When this happens, obviuosly, my session variable for userId is null. My next step would be to create a base controller and imlement OnAuthorizeStarting to check if my Session variable is null or not. This feels kludgy, and I’m hoping there is a better way.

I do not want to have to pass the user Id to the data project as a parameter on every Save call.

Anyone have some brilliance? 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-25T23:11:00+00:00Added an answer on May 25, 2026 at 11:11 pm

    The UserID is available via membership. So, something like this:

    Membership.GetUser(HttpContext.User.Current.UserName).ProviderUserKey
    

    The IPrincipal (HttpContext.user.Current) is created when the user is authenticated against the forms authentication cookie. You just pull up the user id from the Membership whenever you need it.

    If you don’t like accessing Membership every time then you can always create a static helper class that wraps your UserID session call. If it’s null, it goes out to the membership provider, gets it, then stores it in the session.. on later accesses it just retrieves the session variable.

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

Sidebar

Related Questions

Just getting started using MVC in ASP.NET, I'm going to have it so users
Intro I'm developing a project with MVC.Net. I have just started a default website
I have an asp.net MVC project hosting a silverlight application. I just started rebuilding
Just started a 'real world' project using .NET MVC, Ninject, PetaPoco and plan to
I just started with Spring Web MVC. I'm trying to avoid file extenstions in
Have just started playing with ASP.NET MVC and have stumbled over the following situation.
I have just started playing with the ASP.Net MVC framework, and today I created
I've just started a new project in ASP.net 4.0 with MVC 2. What I
I just started a new project, and now with ASP.NET MVC being designed in
I have just started looking into .net MVC and I really like it. There

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.