-1

A user should have unique email instead of UserName. To achieve this I stored email in UserName and UserName in email column of AspNetUsers Table. Now I want to access user name in my view. The method User.Identity.GetUserName() is great, But now I need User.Identity.GetUserEmail(). I can I implement User.Identity.GetUserEmail() ?

Update:

I have to use User.Identity.GetUserEmail() in every view. As I use User.Identity.GetUserId(). I want to write this method in Microsoft.AspNet.Identity namespace so that it will be accessible everywhere.

Irfan Y
  • 1,242
  • 2
  • 21
  • 68

3 Answers3

0

I had to add a new value to the Identity model and to get the new value I did this:

    private string GetUserEmail()
    {
        //Instantiate the UserManager in ASP.Identity system so you can look up the user in the system
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
        //Get the User object
        var currentUser = manager.FindById(User.Identity.GetUserId());
        return currentUser.Email;
    }

You can build this in a relevant controller, or it can be a static function somewhere else, you just need to have a reference to identity.

Personal note: I have to say that I don't fully support the idea to change between the username and email. I think you should consider editing the model instead , this link might help.

LiranBo
  • 2,054
  • 2
  • 23
  • 39
  • I have to use `GetUserEmail()` in every view so defining it in identity model will not work. I have updated the question. Please have a look. – Irfan Y Oct 25 '15 at 07:00
  • you can still use this method for that.. just supply the userid and u get access to all the other fields as well... – LiranBo Oct 25 '15 at 07:03
  • I didn't get it. In your case I have to include identity model namespace within every view to access `GetUserEmail()`. But I want to access this method on including namespace `Microsoft.AspNet.Identity` – Irfan Y Oct 25 '15 at 07:09
  • well in that case u'll need to have `Microsoft.AspNet.Identity` in every view instead. if u still prefer that way just use the code I posted above **in every view**, this is the alternative which is much worse if you ask me. see [this](http://stackoverflow.com/questions/26836169/asp-net-identity-extend-methods-to-access-user-properties) – LiranBo Oct 25 '15 at 07:17
  • btw if u don't like to add this identity model for every view.. just create an external class with a static call (of course you will need to use references to this class instead...) – LiranBo Oct 25 '15 at 07:19
  • I prefer using `Microsoft.AspNet.Identity` in every view because i am already using it. In your case I will have to include another namespace with `Microsot.AspNet.Identity`. I want somehow this method should accommodate in `Microsfot.AspNet.Identity` – Irfan Y Oct 25 '15 at 07:40
0

Let me start by saying that I don't like the idea of executing code in your view. Besides, if you do this everytime, you're breaking the DRY principle.

I think what you need here is the ASP.NET Identity framework. This allows you to customize the authentication & authorization process, including how and where to retrieve user information. By overriding the UserManager class, you can start overriding the methods you want (like GetEmailAsync). You could also modify the CreateUserIdentity method by changing the claims of the identity.

This way you only define your rule once, which you then can use all across your application. But in order to achieve this, you'll have to do some research about ASP.NET Identity yourself or post more accurate information (like your accountcontroller code).

hbulens
  • 1,872
  • 3
  • 24
  • 45
0

When creating a user through the user manager you can apply some custom settings, one of these is requiring an unique email:

var um = new UserManager<User>(new UserStore<User>(new ApplicationDbContext()));
um.UserValidator = new UserValidator<User>(um)
{
    AllowOnlyAlphanumericUserNames = false,
    RequireUniqueEmail = false
};
Jelle Oosterbosch
  • 1,731
  • 15
  • 17
  • I don't want unique email. I just want to put method `GetUserEmail()` in namespace `Microsoft.AspNet.Identity`. So that I can access it like `User.Identity.GetUserEmail()` – Irfan Y Oct 26 '15 at 08:23
  • "A user should have unique email instead of UserName. To achieve this I stored email in UserName and UserName in email column of AspNetUsers Table." It seems to be your are missusing the email and username field, and the way I give you a solution, you can be able to correctly use it! – Jelle Oosterbosch Oct 26 '15 at 08:31