2016-05-07 11 views
1

以前は匿名アクセスを許可していたSPA ASP.NET WebAPIアプリケーションがあります。私は今ASP.Net IDを設定しましたが、アイデンティティ関連のコントローラとアプリケーションの他のコントローラが同時に動作するようにはできません: それはどちらか一方です!ASP.Net WebAPIアプリケーション用のASP.Net IDの設定

スタートアップクラスを追加しました。私のプロジェクトに:

using Test.MyProject; 
using Microsoft.Owin; 
using Microsoft.Owin.Security; 
using Microsoft.Owin.Security.DataHandler.Encoder; 
using Microsoft.Owin.Security.OAuth; 
using Newtonsoft.Json.Serialization; 
using Owin; 
using System; 
using System.Configuration; 
using System.Linq; 
using System.Net.Http.Formatting; 
using System.Web.Http; 

[assembly: OwinStartup(typeof(Test.Client.Startup))] 
namespace Test.Client 
{ 
    public class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      HttpConfiguration httpConfig = new HttpConfiguration(); 

      ConfigureOAuthTokenGeneration(app); 

      ConfigureWebApi(httpConfig); 

      app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 

      //app.UseWebApi(httpConfig); // If this line is commented out my application's controllers work. But then my Account Controller does't work. It if is included, my application's controllers don't work, whilst the Account Controller work 

      GlobalConfiguration.Configure(WebApiConfig.Register); 
     } 

     private void ConfigureOAuthTokenGeneration(IAppBuilder app) 
     { 
      // Configure the db context and user manager to use a single instance per request 
      app.CreatePerOwinContext(ApplicationDbContext.Create); 
      app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
     } 

     private void ConfigureWebApi(HttpConfiguration config) 
     { 
      config.MapHttpAttributeRoutes(); 

      var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First(); 
      jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
     } 
    } 
} 

そして、私はユーザーとロールを管理するためのコントローラを追加した 声明GlobalConfiguration.Configure(WebApiConfig.Register)global.aspx.csでのアプリケーション開始イベントに以前あったが、今同じ場所ですべてを持っているスタートアップクラスに移動しました。

WebApiConfig.Register方法は次のようになります。

public static void Register(HttpConfiguration config) 
{ 
    var container = new UnityContainer(); 
    // Web API configuration and services 

    string appStorageProvider = ConfigurationManager.AppSettings["StorageProvider"]; 
    var provider =(TestComposition.StorageProvider) Enum.Parse(typeof (TestComposition.StorageProvider), appStorageProvider, true); 

    TestComposition.Setup(container, provider); 
    container.RegisterType<GeneralLogger, GeneralLogger>(); 

    container.RegisterType<IExceptionLogger, ExceptionLogger>(); 

    config.EnableCors(); 

    config.DependencyResolver = new UnityResolver(container); 
    config.Services.Add(typeof (IExceptionLogger), container.Resolve<GeneralLogger>()); 

    // Web API routes 
    config.MapHttpAttributeRoutes(); 
} 

AccountController私の新しいでは、私は私がスタートアップクラスに設定さOwinContextからApplicationUserManagerを取得することを可能にするコードを持っています。私のアプリケーション上に示したようにコメントアウトapp.UseWebApi(httpConfig)

protected ApplicationUserManager AppUserManager 
{ 
    get 
    { 
     return _AppUserManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
    } 
} 

それが使用されるように動作します。 「HttpRequestMessage」 は「GetOwinContext」と拡張子なしの方法 の定義が含まれていない「GetOwinContext」受諾:

Request.GetOwinContext()エラーCS1061:私は私の新しいAccountController上の任意のアクションを呼び出す場合しかし、私はこれを取得します私はapp.UseWebApi(httpConfig)声明AccountController作品にコメントしたが、その後、私の他のコントローラがない場合タイプ の最初の引数は「HttpRequestMessage」(あなたがusingディレクティブ またはアセンブリ参照が不足している?)

を見つけることができます作業。ここで私は、これらのようなエラーが発生します:

{ 「メッセージを」:「エラーが発生しました。」、 「exceptionMessage」:「型 『TestController』のコントローラを作成しようとすると、エラーが発生したことを確認してください。コントローラは パラメータなしpublicコンストラクタを持っていること " "exceptionType。": "のSystem.InvalidOperationException"、 "スタックトレース":" System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 要求、HttpControllerDescriptor controllerDescriptor、タイプで controllerType)\ r \ n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpReques tMessage リクエスト)\ R \ nは System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()」、 "のInnerExceptionに":{ "メッセージ": "エラーが発生しました"、 "exceptionMessage"。 : "Type 'MyProject.Api.TestController'に既定のコンストラクターがありません"、 "exceptionType": "System.ArgumentException"、 "stackTrace": "at System.Linq.Expressions.Expression.New(型の型)\ r \ nで System.Web.Http.Internal.TypeActivator.Create [TBase](種類 instanceType)\ r \ nを に設定します。System.Web.Http.Dispatcher.DefaultHttpControllerActivator。GetInstanceOrActivator(HttpRequestMessage 要求タイプcontrollerType、Func`1 &ベーター)\ R \ nは System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 要求、HttpControllerDescriptor controllerDescriptor、タイプ controllerType)における」 }}

任意のアイデアは、ここで何が起こっているの?

答えて

1

問題がOWINでWEBAPIを設定するための起動時に、あなたが同じHttpConfigurationインスタンスを使用していないということである。

この方法では、OWIN Web APIミドルウェアはUnityContainerの知識がなく、デフォルト実装を使用します。このため、コントローラの作成に失敗しました。

のWeb API設定とUnityContainer登録の両方に同じHttpConfigurationを使用してください:

public class Startup { 
    public void Configuration(IAppBuilder app) { 
     ConfigureOAuthTokenGeneration(app); 

     ConfigureWebApi(app); 

     app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);    
    } 

    private void ConfigureOAuthTokenGeneration(IAppBuilder app) { 
     // Configure the db context and user manager to use a single instance per request 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
    } 

    private void ConfigureWebApi(IAppBuilder app) { 
     // configure Web Api 
     GlobalConfiguration.Configure(WebApiConfig.Register); 
     // Manually assign httpConfig from GlobalConfiguration 
     HttpConfiguration httpConfig = GlobalConfiguration.Configuration; 
     // Use same config with OWIN app 
     app.UseWebApi(httpConfig); 
    } 
} 

あなたが複数の場所でのWeb APIを構成しています。 WebApiConfig.Registerメソッドは、HttpConfigurationのために設定したいものすべてを統合する必要があります。

public static void Register(HttpConfiguration config) { 
    var container = new UnityContainer(); 
    // Web API configuration and services 

    string appStorageProvider = ConfigurationManager.AppSettings["StorageProvider"]; 
    var provider =(TestComposition.StorageProvider) Enum.Parse(typeof (TestComposition.StorageProvider), appStorageProvider, true); 

    TestComposition.Setup(container, provider); 
    container.RegisterType<GeneralLogger, GeneralLogger>(); 

    container.RegisterType<IExceptionLogger, ExceptionLogger>(); 

    config.EnableCors(); 

    config.DependencyResolver = new UnityResolver(container); 
    config.Services.Add(typeof (IExceptionLogger), container.Resolve<GeneralLogger>()); 

    // Web API routes 
    config.MapHttpAttributeRoutes(); 

    // configure formatter 
    var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First(); 
    jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
} 
+0

あなたは本当に正しいです!上のコードはちょうどうまく動作します:-) ありがとうございました –

+1

@ HankReardenこれが正しい場合は、これを正しい答えとして受け入れることを検討してください(十分な評判ポイントを取得した場合、upvote) – DavidG

関連する問題