1

Is there any built in way to get a users email address based on their username in ASP.NET 4.0? Or do I have to query the necessery tables?

I'm using this to get the logged in user:

string username = HttpContext.Current.User.Identity.Name.ToString();

Is there similar functionality to get the currently logged in users email from the database?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jimmy Collins
  • 3,294
  • 5
  • 39
  • 57

1 Answers1

8

You can use the Membership.GetUser method, which takes the username. This returns a MembershipUser object, which has an Email property. So, something like this:

var user = Membership.GetUser(username);
var email = null;

if (user != null)
{
    email = user.Email;
}

System.Web.Security moved to the System.Web.ApplicationServices assembly in .NET 4.0 Framework, so you manually need to add a reference to that assembly to access Membership.

Campi
  • 1,932
  • 1
  • 16
  • 21
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • 2
    If you need an email of current user you don't need to pass username, just call Membership.GetUser() – frennky Feb 22 '11 at 15:09