iamserious
iamserious

Reputation: 5475

How to get username in global asax?

I am trying to check if the user belongs to someone's friendlist from the database and redirect him accordingly.

I am doing this in a routehandler called by Global Asax.

I just want to know how to get the username (from the login information) in the route handler class (or Global asax)

I used this:

string username = HttpContext.Current.User.Identity.Name;  

and very strangely, its assigning ".aspx" as the username!!

ps: i did search for similar question but in vain. sorry if I dint search it thoroughly.

Upvotes: 4

Views: 11544

Answers (3)

philw
philw

Reputation: 687

global.asax Session_Start is called when a session is started, which is, say, when a browser hits your site. The browser user has not generally logged on at that point, so there's not going to be any HttpContext.Current.User.Identity.Name for you to grab. You should get an empty string.

An exception would be if the user was already authenticated and the session re-started for some reason, for example if you bounced the server or if the session time-out was shorter than your authentication time-out. But in the general case, the sequence must be:

  1. session starts
  2. user logs in and HttpContext.Current.User.Identity.Name becomes available

So the answer to your question is: "in the general case, you can't".

Upvotes: 5

Joan Wojcicki Fortney
Joan Wojcicki Fortney

Reputation: 85

Check your web.config file and look for the section and make sure that authentication mode is set to windows, like this:

<authentication mode="Windows"></authentication>

Upvotes: 3

Ed B
Ed B

Reputation: 6054

It should work...must be something in your authentication method.

How do you have it setup?

It looks like you are doing the authentication yourself and assigning the identity from the wrong server variable

Upvotes: 5

Related Questions