0

I have to customize ASP.NET Identity entities.

I have these classes:

<!-- language: c# -->

public class ApplicationUser: IdentityUser<string, ApplicationLogin, ApplicationUserRole, ApplicationClaim> {}

public class: UserStore<ApplicationUser, ApplicationRole, string, ApplicationLogin, ApplicationUserRole, ApplicationClaim> {}

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

How can I create instance of IUserStore<ApplicationUser> for DI in this case?


I can't derive my DbContex from IdentityDbContext because of using unit of work repository. I have found similar question here.

1

1 Answer 1

1

You will need to create some another classes and interfaces:

public interface IApplicationUserStore : IUserStore<ApplicationUser, string>
{

}

public class ApplicationUserStore :
    UserStore<ApplicationUser, ApplicationRole, Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>,
    IApplicationUserStore
{
    public ApplicationUserStore()
        : base(new ApplicationDbContext())
    {

    }

    public ApplicationUserStore(ApplicationDbContext context)
        : base(context)
    {

    }
}

And create class ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationDbContext()
        : base("AuthenticationConnectionString")
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    ...
}

then from your ApplicationUserManager you would be able to call

var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>()));

And this is the complete class ApplicationUserManager that receives IApplicationUserStore in the constructor:

public class ApplicationUserManager : UserManager<ApplicationUser, Guid>
    {
        public ApplicationUserManager(IApplicationUserStore store)
            : base(store)
        {
        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>())) { PasswordHasher = new CustomPasswordHasher() };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = false,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = false,
            };

            return manager;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.