ASP.net Identity, IoC and sharing DbContext

2019-03-11 12:32发布

问题:

Have anyone been successful in attaching an IoC with OWIN ASP.NET Identity to share the same DbContext as the WebApi (or MVC) application?

I would like that if ASP.Net identity loads the user, it loads in the same dbcontext as used in the rest of the application.

(using AutoFac as IoC - but wouldn't mind an example with other container)

Edit: 06/Jan/2014: Just to be a bit more specific, when the ASP.Net identity attempts to use a DbContext it needs to be the same DbContext instance as the rest of the application. Otherwise it will create two instances of the same DbContext. I am already using the .PerHttpApiRequest() extension.

The problem I found is that when setting up OWIN classes I couldn't find a way to: 1) attach the container to the OWIN pipeline; 2) tell OWIN to resolve the classes it needs using that container so it can share the same scope (i.e. OAUthServerOptions or any other that may contain other dependencies)

Having said that, how can you setup the ASP.Net Identity resolving the two points above?

回答1:

EDIT: added instantiating the OWIN pipeline as per comment.

As the IdentityDbContext used by ASP.NET Identity inherits from DbContext, you can simply create your own implementation inheriting from IdentityDbContext like so

public class ApplicationContext : IdentityDbContext<ApplicationUser>
{
     //additional DbSets
    public DbSet<OtherEntity> OtherEntities { get; set; } 
}

public class ApplicationUser : IdentityUser
{
    //any custom properties you want for your user.
}

public class OtherEntity
{
    [Key]
    public int Id { get; set; }
}

now as far as using IoC, from a unity perspective, I register an instance of the UserStore like so

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new InjectionConstructor(typeof(ApplicationContext)));
}

then create a wrapper around the user manager

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

and inject that wherever I need a user manager.

As for the OWIN pipeline, you need to instantiate the OAuthAuthorizationProvider through your container, like so.

container.RegisterType<IOAuthAuthorizationServerProvider, ApplicationOAuthProvider>(new InjectionConstructor("self", typeof(ApplicationUserManager)));

then for example in WebApi, you need to simply get the instance from the container in startup.

static Startup()
        {
            OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/Token"),
                Provider = (IOAuthAuthorizationServerProvider)GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IOAuthAuthorizationServerProvider)),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(5),
                AllowInsecureHttp = true,
                RefreshTokenProvider = new SimpleRefreshTokenProvider()
            };
        }