-2

When creating another model Testimonial I store the active user's Id as the Author which is a string looking like: 00b9c6aa-010c-4cbd-ac16-c72d05e7906a. In another view I want to display the name and other user information associated with that Id for each Testimonial. The view will contain information from many different users, not just the actively logged one. I cannot figure out how to get a user their information by Id.

How do I get the IdentityUser by Id?

All of the tables containing the users are default plus some additional columns. Within the table named AspNetUsers, I've got Id, FirstName, Email, etc. The Id is the key value and I would like to know how to (within the controller for Testimonial) retrieve the information for a given Id.

Samuel Davidson
  • 783
  • 6
  • 16
  • I assume that you have extended the default context that is generated by the MVC project template? Have you created your new table - if so, please add to the question. – Brendan Green May 19 '15 at 02:30
  • I have appended the question. – Samuel Davidson May 19 '15 at 02:38
  • Yes, but you created a `Testimonial` table that contains the `Id` of the user that created it. Was this done as a foreign key to the `ApplicationUser` table? – Brendan Green May 19 '15 at 03:05
  • No. I am new to ASP.Net and SQL. If I changed it to be the foreign key, would there be an easy way to get each user's information? – Samuel Davidson May 19 '15 at 03:09
  • Yes - you would be able to query the `Testimonial` table, and retrieve the associated user details in a single database operation. How do you query your `Testimonial` table? Is it via Entity Framework? – Brendan Green May 19 '15 at 05:36

1 Answers1

3

IdentityConfig.cs // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.

public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };
        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false,
        };
        manager.EmailService = new EmailService();
        var dataProtectionProvider = options.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }
}
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) :
        base(userManager, authenticationManager) { }

    public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
    {
        return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
    }

    public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
    {
        return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
    }
}

In your controller define the following properties

    private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
    private ApplicationSignInManager _signInManager;

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set { _signInManager = value; }
    }

You can use the following method in the controller

var user = await UserManager.FindByIdAsync("00b9c6aa-010c-4cbd-ac16-c72d05e7906a");

You can get the logged in user like the following

var user = await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync());
Muhammad Nagy
  • 216
  • 1
  • 7
  • This only gets the logged in user. I believe the poster wants to get the user details that relate to records in some other table. – Brendan Green May 19 '15 at 05:34
  • var user = await UserManager.FindByIdAsync("00b9c6aa-010c-4cbd-ac16-c72d05e7906a"); works with any user Id while var user = await UserManager.FindByIdAsync(await SignInManager.GetVerifiedUserIdAsync()); used for the logged in user only. – Muhammad Nagy May 19 '15 at 09:51