结构图Web Api 2帐户控制器和个人帐户

Rog*_*ger 7 c# structuremap owin asp.net-web-api2

我非常喜欢IOC和web-api 2,但是我已经在我的web-api 2中使用了StructureMap来处理我自己的控制器.我没有管理的是使用个人帐户在AccountController上使用StructureMap.我使用AccountController开箱即用,到目前为止我所管理的是:

  1. 在Ioc.cs中我添加了以下内容(由于错误)

      x.For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>();
      x.For<DbContext>().Use(() => new ApplicationDbContext());
      x.For<ISecureDataFormat<AuthenticationTicket>()
           .Use<SecureDataFormat<AuthenticationTicket>>();
    
    Run Code Online (Sandbox Code Playgroud)

但是现在我遇到了这个错误:

"没有注册默认实例,无法自动确定类型IDataSerializer <AuthenticationTicket>"

我真的不知道该怎么做.我试图找到IDataSerializer的详细实例,但没有运气.

顺便说一句......我已经安装了Nuget包"Structuremap.webapi2"

小智 8

解决了!

将此配置添加到IoC.cs或DefaultRegistry:

        For<ISecureDataFormat<AuthenticationTicket>>().Use<SecureDataFormat<AuthenticationTicket>>();
        For<IDataSerializer<AuthenticationTicket>>().Use<TicketSerializer>();
        For<IDataProtector>().Use(() => new DpapiDataProtectionProvider().Create("ASP.NET Identity"));
        For<ITextEncoder>().Use<Base64UrlTextEncoder>();

        For<Microsoft.AspNet.Identity.IUserStore<ApplicationUser>>().Use<Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>>();
        For<System.Data.Entity.DbContext>().Use(() => new ApplicationDbContext());
Run Code Online (Sandbox Code Playgroud)