0

我正在使用IdentityServer3AspnetIdentity 进行用户存储。Identity Server 在此处使用依赖注入作为详细信息

我有 ASP.NET Web API 2,我想在控制器中注入某些服务。

[RoutePrefix("api/v1")]
public class UsersController : ApiController
{
    private readonly ApplicationUserManager _userManager;

    public UsersController(ApplicationUserManager userManager)
    {
        _userManager = userManager;
    }

    [HttpGet]
    [Route("users/{userName:maxlength(75)}")]
    public async Task<IHttpActionResult> GetUser([FromUri]string userName)
    {
        // use _userManager here
    }
}

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
}

public class ApplicationUser : IdentityUser
{           
}

然后在 OWIN startup.cs

 [assembly: OwinStartup(typeof(Api.Startup))]
 namespace Api
{
  public class Startup
  {
    internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
    public void Configuration(IAppBuilder app)
    {
        DataProtectionProvider = app.GetDataProtectionProvider();

        
        app.Map("/api/v1/users", adminApp =>
        {
            var factory = new IdentityManager.Configuration.IdentityManagerServiceFactory();


            factory.Register(new Registration<ApplicationDbContext>(resolver => new                                     ApplicationDbContext(connectionString)));
            factory.Register(new Registration<ApplicationUserStore>());
            factory.Register(new Registration<ApplicationRoleStore>());
            factory.Register(new Registration<ApplicationUserManager>());
            factory.Register(new Registration<ApplicationRoleManager>());
            factory.Register(new Registration<IDataProtectionProvider>(resolver => Startup.DataProtectionProvider));
            factory.IdentityManagerService = new Registration<IIdentityManagerService, AspNetIdentityConfigurationService>();


            adminApp.UseIdentityManager(new IdentityManagerOptions()
            {
                Factory = factory
            });
        });
    }
  }
} 

有了这个我得到了错误An error occurred when trying to create a controller of type 'UsersController'. Make sure that the controller has a parameterless public constructor. 所以我认为DefaultHttpControllerActivator不知道注册的依赖以及如何注入它们。

我在这里有什么选择?如何访问 UserController 中的注册服务

4

0 回答 0