1

I am trying to implement a custom UserStore in Asp .Net 5 beta 7 based on the idea presented over here.

Here is the code for my custom user store:

public class ApplicationUserStore : UserStore<ApplicationUser>
{
    public ApplicationUserStore(DbContext context)
        : base(context)
    {
    }


    public async override Task<ApplicationUser> FindByIdAsync(string userId)
    {
        var user = await base.FindByIdAsync(userId);
        this.Context.Entry(user).Reference(u => u.Subject).Load();
        return user;
    }
}

I am encountering two issues with this code.

  1. No method FindByIdAsync to override in the base class
  2. Inside FindByIdAsync, the method Reference is not found

I am also not sure how I would go about configuring the custom user store once I get it to compile.

UPDATE:

The first issue I have solved by changing the definition of the FindByIdAsync override to the following:

public async override Task<ApplicationUser> FindByIdAsync(string userId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))

The second issue is still a problem. How do I specify in EF7 that I wish to explicitly load the reference property Subject of my ApplicationUser entity? In EF6 the following is defined in the namespace System.Data.EntityFramework.Infrastructure:

public DbReferenceEntry<TEntity, TProperty> Reference<TProperty>(Expression<Func<TEntity, TProperty>> navigationProperty) where TProperty : class;

Where is this extension method defined in EF7? Or is there a new method for explicitly loading references?

Community
  • 1
  • 1
BruceHill
  • 6,954
  • 8
  • 62
  • 114
  • 1
    Wait for a week (more...), they're going to change all structures again, it will be Identity v.5 and will let you configure your authentication creating only 20 classes and overriding 80 methods... (ah, and don't forget interfaces, it has more than a thousand). And the best, this time it will be well documented. – Vi100 Nov 25 '15 at 09:42

1 Answers1

0

That question you are referring to is very old and a lot has changed in Identity since then.

I can tell you I have implemented UserStore in my project without using entity framework and my class is declared like this:

public sealed class UserStore<TUser> :
    IUserStore<TUser>,
    IUserPasswordStore<TUser>,
    IUserEmailStore<TUser>,
    IUserLoginStore<TUser>,
    IUserRoleStore<TUser>,
    IUserClaimStore<TUser>,
    IUserPhoneNumberStore<TUser>,
    IUserLockoutStore<TUser>,
    IUserTwoFactorStore<TUser>
   where TUser : SiteUser
{
.. implementation
}

where SiteUser is my own custom class

you may also find this question helpful as it shows how I plugged in my custom userstore in DI

looks like you do want to use EF but the principals of how to declare and plug in a custom userstore should be the same with or without a dependency on EF

Community
  • 1
  • 1
Joe Audette
  • 35,330
  • 11
  • 106
  • 99
  • Thanks, Joe for the information. The question that you linked will I think help for configuring the user store once it compiles. Yes, I am using Entity Framework. I am essentially trying to solve the same problem in the question that I linked to; namely that the user object being returned by FindByIdAsync does not have the reference properties loaded. – BruceHill Sep 27 '15 at 22:21