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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:12:55+00:00 2026-05-27T16:12:55+00:00

After a user Logs in to the system, i need to put a variable

  • 0

After a user Logs in to the system, i need to put a variable in the Session. I am doing this with an extension method:

Login method:

[HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                    int id = this.HttpContext.GetFirmaId();
                    String username = this.HttpContext.User.Identity.Name;

                    //SessionExtensions.SetFirmaId(Session, model.UserName);

                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        if (User.IsInRole("Administrator"))
                        {
                            RedirectToAction("Index", "Admin");
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Brugernavn eller password er forkert.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

Extension method:

public static int GetFirmaId(this HttpContextBase context)
    {

        var firmaid = context.Session["firmaid"] as int?;

        if (firmaid == null)
        {
            // Hvis firmaid er røget ud af Session, skal den firmaid som tilhører brugeren hentes

            firmaid = SetFirmaId(context.Session, context.User.Identity.Name);

        }

        return (int)firmaid;
    }

The problem is that context.User.Identity.Name returns an empty string.

How can i solve this problem?

  • 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-27T16:12:56+00:00Added an answer on May 27, 2026 at 4:12 pm

    try

    public static int GetFirmaId(this HttpContextBase context)
            {
    
                var firmaid = context.Session["firmaid"] as int?;
    
                if (firmaid == null)
                {
                    // Hvis firmaid er røget ud af Session, skal den firmaid som tilhører brugeren hentes
    
                    firmaid = SetFirmaId(context.Session, context.User.Identity.Name);
    
                }
    
                return (int)firmaid;
            }
    

    UPDATE – as per comment: use it just like this.HttpContext.GetFirmaId() from your controller.

    Some useful links:

    • Extension Methods
    • http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.aspx
    • http://msdn.microsoft.com/en-us/library/system.web.httpcontextbase.aspx

    UPDATE 2 – as per comments:

    The problem of being User.Identity.Name has no connection to the question – the answer above is what to do to access it in a class outside of a Controller.

    AS the OP saw in the debugger it is even empty inside the controller in this specific case – I suspect that it is being accessed “too early” in the process so that it is indeed empty.

    If you need help dealing with AccountController ask a new question.

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

Sidebar

Related Questions

In this website I need a system that logs the user out after 10
In a Swing app a method should continue only after user enters a correct
I have a form that and after the user fills in this form, I
I need to do some processing after my user leaves a text field. I
I'm designing a question-and-answer Ruby on Rails application. After a user logs in, you
Essentially I'm trying to set a cookie after a user logs in to persist
After user places an order I have to send detailed email message containing order
Details: Only disable after user clicks the submit button, but before the posting back
Is it possible to get shutdown reason in Windows Server 2008 immediately after user
After the user fills my backing bean with info through the forms, I want

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.