Jen
Jen

Reputation: 1

Asp.net Identity with Dependency Injection

I'm applying asp.net identity with repository pattern and having some trouble. In Unity, I see it register as below (it's runned):

container.RegisterType<IUserStore<IdentityUser, Guid>, UserStore>(new TransientLifetimeManager());
container.RegisterType<RoleStore>(new TransientLifetimeManager());

Now I want to register by using Autofac, especial the first register code but I can't find anything about this. If you have another solution for apply asp net identity with repository pattern.

Upvotes: 0

Views: 1514

Answers (1)

tacos_tacos_tacos
tacos_tacos_tacos

Reputation: 10585

Some examples I found on the Internet.

1. Registering roles, users, associations

https://github.com/kirill-vinnichek/BerezovskyVinnichek.Wunderlist/blob/master/Wunderlist/Epam.Wunderlist.Web/App_Start/AutofacConfig.cs

public static class AutofacConfig
{
    public static void Config()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterFilterProvider();
        builder.RegisterModule(new AutofacDataModule());
        builder.RegisterModule(new AutofacServiceModule());

        builder.RegisterType<WunderlistUserStore>().As<IUserStore<OwinUser,int>>().InstancePerRequest();
        builder.RegisterType<WunderlistRoleStore>().As<IRoleStore<OwinRole,int>>().InstancePerRequest();
        builder.RegisterType<WunderlistUserManager>().As<UserManager<OwinUser,int>>().InstancePerRequest();

        IContainer container = builder.Build();

        System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    }
}

2. Registering user store

https://github.com/cococrm/ZY.Web/blob/master/ZY.WebApi/Autofac/RepositoryModule.cs

    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterGeneric(typeof(Repository<,>)).As(typeof(IRepository<,>));
        builder.RegisterType<UserStore>().As<IUserStore<User,int>>();
    }

Upvotes: 2

Related Questions