get current user email in Identity

24,964

Solution 1

You could do something on these lines:

public static class IdentityExtensions
{
    public static string GetEmailAdress(this IIdentity identity)
    {
        var userId = identity.GetUserId();
        using (var context = new DbContext())
        {
            var user = context.Users.FirstOrDefault(u => u.Id == userId);
            return user.Email;
        }
    }        
}

and then you will be able to access it like:

this._identity.GetEmailAdress();

Solution 2

You can get the current user in ASP.NET Identity as shown below:

ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
    .GetUserManager<ApplicationUserManager>()
    .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

//If you use int instead of string for primary key, use this:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext()
    .GetUserManager<ApplicationUserManager>()
    .FindById(Convert.ToInt32(System.Web.HttpContext.Current.User.Identity.GetUserId()));


For getting custom properties from AspNetUsers table:

ApplicationUser user = UserManager.FindByName(userName);
string mail= user.Email;

Hope this helps...

Share:
24,964
Soheil Alizadeh
Author by

Soheil Alizadeh

Updated on July 09, 2022

Comments

  • Soheil Alizadeh
    Soheil Alizadeh almost 2 years

    I use IIdentity interface For Getting current userid and username.

    so implement following Method:

    private static IIdentity GetIdentity()
    {
        if (HttpContext.Current != null && HttpContext.Current.User != null)
        {
            return HttpContext.Current.User.Identity;
        }
    
        return ClaimsPrincipal.Current != null ? ClaimsPrincipal.Current.Identity : null;
    }
    

    And Add this code: _.For<IIdentity>().Use(() => GetIdentity()); in my IoC Container[structuremap].

    Usage

    this._identity.GetUserId();
    this._identity.GetUserName();
    this._identity.IsAuthenticated
    

    Now I Want to Implement GetEmailAdress Method, How To do this?

    Example

    this._identity.GetEmailAdress();
    

    When use this._identity.GetUserName(); do not get username form database.

  • Soheil Alizadeh
    Soheil Alizadeh about 7 years
    when use this._identity.GetUserName(); do not get username from database.
  • Jinish
    Jinish about 7 years
    Yeah, beacause Username is a part of your Principal, but not email. You would have to get email from the DB, unless you are using claims in your application. In which case you could set it in the claims when authenticating the user and later read it from claims.
  • Soheil Alizadeh
    Soheil Alizadeh about 7 years
    thanks, @MuratYıldız I use ASP.NET Identity but I want to get EmailAdress like this._identity.GetUserName(); that do not get it from database.
  • Murat Yıldız
    Murat Yıldız about 7 years
    @SoheilAlizadeh Could you try to update implemented method with the related lines on my answer? Maybe it fix the problem and you can achieve to use the implemented method successfully.
  • Soheil Alizadeh
    Soheil Alizadeh about 7 years
    Ok, you can show me how to do this or edit your answer,... ?
  • Soheil Alizadeh
    Soheil Alizadeh about 7 years
    I think, can fix this problem with claims.
  • Panagiotis Kanavos
    Panagiotis Kanavos about 7 years
    @SoheilAlizadeh the answer already shows that. The identity of a user is not the same as his/her profile properties. You can't get the email from the identity, any more than one can get your email from your passport or driver's license. Some sites use an email address as the user name. They still allow you to change the contact email though, without changing your user name
  • Panagiotis Kanavos
    Panagiotis Kanavos about 7 years
    @SoheilAlizadeh the user's name is one thing. The user's email is another. You can't get profile information from an identity, it's like asking to get one's email from his passport, identity card or driver's license. There's nothing to fix here - claims are still a form of identity. Besides, you can have one email address as your user name, and another as your contact email