Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have authentication code:

var authTicket = new FormsAuthenticationTicket(/*blahblah....*/);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, 
                            FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);
var name = HttpContext.User.Identity.Name; // line 4

By putting in debug statements, I find that name on line 4 is empty. But the next time I make a call on this browser session, the HttpContext.User.Identity.Name is correctly set.

So when does this value get set?

just wondering why you would manually create the cookie and add it to the response instead of just calling: FormsAuthentication.SetAuthCookie()? – Nick Meldrum Jul 6, 2011 at 11:26 Also wondering why you would need to get the username from the HttpContext if you are about to set an Auth cookie? Surely if you are about to set an Auth cookie - you are saying that you want to set this person as "logged in" - how can you do that if you don't already know their username? – Nick Meldrum Jul 6, 2011 at 11:30 @Nick - reason is because this is the latest in a series of developments, which you can follow back from here: stackoverflow.com/q/6586156/7850 – Shaul Behr Jul 6, 2011 at 12:21 Now that's what I would have thought. Except that I'm experiencing behavior that doesn't conform to what you're describing. See here: stackoverflow.com/q/6586156/7850 – Shaul Behr Jul 6, 2011 at 12:38 Wow, man. That seems to be a truly puzzling problem you have there, but as I have gathered from the conversation, the cookie is being sent with the response after the login, also within the air client. I will try to see if I can help with your initial problem. – tehshin Jul 6, 2011 at 13:05 is the authentication cookie set in both forms authentication and Windows authentication, or just forms authentication? – JustBeingHelpful Aug 10, 2012 at 19:12

From your code it looks like you either would have to call:

FormsAuthentication.Authenticate(name, password)

or, if using Membership the following:

Membership.ValidateUser(name, password)
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.