Seong Kim
Seong Kim

Reputation: 605

How do I get current user another property asp mvc 5

I have a situation.

AspNetUsers has a column called AccType which is int type

If AccType is 1 = customer

If AccType is 2 = manager.

I can access to current logged user id by User.Identity.GetUserId()

I am trying to do if statment as following:

    if(User.Identity.GetUserId().AccType == 1){
       redirect to specific page..
    }elseif (User.Identity.GetUSerId().AccType == 2){
     redirect to 2nd specific page..
    }

User.Identity.GetUserId().AccType this is now working.

Please help! Thanks!

Upvotes: 0

Views: 523

Answers (1)

Hristo Stoichev
Hristo Stoichev

Reputation: 195

User.Identity.GetUserId() returns the id of the currently logged in user. However, if you want to access its properties, you need to get the actual user object. One way to to this is the following:

var userId = this.User.Identity.GetUserId();
var user = this.Request.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(userId);

A more convenient way will be to make a property for the ApplicationUser in your controller and then to reuse the code.

Property:

public ApplicationUserManager UserManager
{
    get
    {
        return this.Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
    }
}

Getting the user:

var user = this.UserManager
    .FindById(this.User.Identity.GetUserId());

Upvotes: 1

Related Questions