Search code examples
asp.netajaxforms-authentication

asp.net get user information from xmlhttp requests


I am working on an application where certain actions are performed using ajax requests to a backend api page, such as adding likes to content. The javascript function will pass the user ID and content ID to the api page in an xmlhttp request.

We use Forms Authentication with Active Directory.

For obvious reasons, I want to verify that the input I receive on the api page before doing anything at all.

While my web.config makes sure that only logged in users can access the api page, I can't find any way to get the current user's info such as username. I tried the following:

Membership.GetUser().Username;

How do I go about getting the logged in users' information from my page?

thanks


Solution

  • HttpContext.Current.User returns an object of type IPrincipal, and IPrincipal.Identity returns an object of type IIdentity.

    HttpContext.Current.User.Identity.Name gives you the username

    HttpContext.Current.User.Identity.IsAuthenticated gives you the authentication status (boolean)

    Armed with that, you can look up other details in the database, possibly storing them in the Session.