ASP.net身份,IoC和共享DbContext [英] ASP.net Identity, IoC and sharing DbContext

查看:84
本文介绍了ASP.net身份,IoC和共享DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人成功使用OWIN ASP.NET Identity附加IoC来共享与WebApi(或MVC)应用程序相同的DbContext吗?

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

我希望如果ASP.Net身份加载用户,则它将在应用程序其余部分中使用的dbcontext中加载.

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.

(使用AutoFac作为IoC-但不介意使用其他容器作为示例)

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

修改: 2014年6月6日:更具体一点,当ASP.Net身份尝试使用DbContext时,它需要与应用程序的其余部分相同的DbContext instance .否则,它将创建相同DbContext的两个实例.我已经在使用.PerHttpApiRequest()扩展名.

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.

我发现的问题是,在设置OWIN类时,我找不到以下方法: 1)将容器连接到OWIN管道; 2)告诉OWIN使用该容器解析所需的类,以便它可以共享相同的作用域(即OAUthServerOptions或可能包含其他依赖项的任何其他作用域)

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)

话虽如此,如何设置ASP.Net Identity来解决以上两点?

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

推荐答案

添加了根据注释实例化OWIN管道的方法.

added instantiating the OWIN pipeline as per comment.

由于ASP.NET Identity使用的IdentityDbContext继承自DbContext,因此您可以像这样简单地创建继承自IdentityDbContext的自己的实现

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; }
}

就统一角度而言,现在就使用IoC而言,我像这样注册UserStore的实例

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.

对于OWIN管道,您需要像这样通过容器实例化OAuthAuthorizationProvider.

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)));

然后例如在WebApi中,您只需在启动时从容器中获取实例.

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()
            };
        }

这篇关于ASP.net身份,IoC和共享DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆