CreatePerOwinContext在owin自托管web-api中导致404响应

时间:2014-11-05 17:05:14

标签: asp.net oauth-2.0 owin asp.net-web-api2 self-hosting

我有一个自托管的asp.net web api。我有几个api控制器工作正常。所有请求都达到api方法并响应到达客户端。 现在我尝试为web-api添加授权。

我通过添加AUTH SECTION更改了我的启动配置。

   static void Main(string[] args)
        {
            StartOptions startOptions = new StartOptions();
            startOptions.Urls.Add("https://*:44305/");
            startOptions.Urls.Add("http://*:9001/");
            using (Microsoft.Owin.Hosting.WebApp.Start<dobStartUp>(startOptions))
            {
              //...
            }
       }
    public class dobStartUp
    {
        public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }

        public static string PublicClientId { get; private set; }

        public void Configuration(IAppBuilder app)
        {
            //----AUTH SECTION---
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
           // PublicClientId = "self";
           // OAuthOptions = new OAuthAuthorizationServerOptions
           // {
           //     TokenEndpointPath = new PathString("/Token"),
           //     Provider = new ApplicationOAuthProvider(PublicClientId),
           //     AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
           //     AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
           //     AllowInsecureHttp = true
           // };
           // app.UseOAuthBearerTokens(OAuthOptions);
            //-------------

            var config = new HttpConfiguration();
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            config.Services.Replace(typeof(IAssembliesResolver),new dobAssembliesResolver());
            config.Formatters.Clear();
            config.Formatters.Add(new JsonMediaTypeFormatter());
            config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
           // config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Common.dobJsonContractResolver();
           var container = new UnityContainer();
            container.RegisterType<IdobRDBRepository, dobRDBRepository>(new TransientLifetimeManager());
            container.RegisterType<IResourcesContentRepository, AS3ContentRepository>(new TransientLifetimeManager());
            SystemDiagnosticsTraceWriter traceWriter = config.EnableSystemDiagnosticsTracing();
            traceWriter.IsVerbose = false;
            traceWriter.MinimumLevel = TraceLevel.Debug;
            config.DependencyResolver = new UnityDependencyResolver(container);
            config.MapHttpAttributeRoutes();
            app.UseWebApi(config);
        }
    }

当我在auth-section中使用app.CreatePerOwinContext时,会调用用于创建appDbContext和appUserManager的方法,但我对每个api方法都有404响应。当我不调用这些CreatePerOwinContext方法时,每个人都像往常一样工作。我知道我可以使用我的IoC容器来创建dbContext和userManager的实例,但我想了解app.CreatePerOwinContext当前的问题并且404响应。 提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

我找到了404响应的原因。它是由于自定义程序集解析器。我有控制器在单独的DLL中,这就是为什么我使用自定义程序集解析器,我手动加载与api控制器组装。这很好用,直到我在owin管道中只使用了web-api。 接下来我尝试向web api添加授权 - 我使用[CreatePerOwinContext]和[app.UseOAuthBearerTokens],所以我添加了中间件。它在web-api开始创建任何控制器之前加载引用的程序集。如果我在debuging时理解我已经两次加载了api-controllers程序集。因此,它在创建控制器实例时会导致冲突。

相关问题