解决内部OWIN回调委托

时间:2015-04-08 02:24:46

标签: c# dependency-injection autofac

在OWIN回调委托中解析对象的正确方法是什么?

假设AutofacConfig.AutofacContainerIContainerIServiceManager已注册为InstancePerRequest

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    Provider = new CookieAuthenticationProvider
    {
        OnResponseSignIn = cookieSignInCtx =>
        {
            // this?
            using (var scope = AutofacConfig.AutofacContainer.BeginLifetimeScope("AutofacWebRequest"))
            {
                var serviceManager = scope.Resolve<IServiceManager>();
            }

            // or this?
            var serviceManager = DependencyResolver.Current.GetService<IServiceManager>();

            // or something else?
        }
    }
});

1 个答案:

答案 0 :(得分:0)

我们解决了 IOAuthAuthorizationServerProvider 提供商的问题,而不是为您解决问题。类似(如果不相同)的想法;)。

const string publicClientId = "self";
var prod = container.Resolve<IOAuthAuthorizationServerProvider>(new NamedParameter("publicClientId", publicClientId));

var oAuthOptions = new OAuthAuthorizationServerOptions
{
    TokenEndpointPath = new PathString("/Token"),
    Provider = prod,
    AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp = true
};

提供者是这样注册的:

containerBuilder.RegisterInstance(new IdentityFactoryOptions<ApplicationUserManager>
{
    DataProtectionProvider = new DpapiDataProtectionProvider("MyWebAPI")
});