身份2.2.1&带有Ninject的UserManager

时间:2017-08-30 17:25:52

标签: c# asp.net-web-api ninject asp.net-identity-2

我一直在Web API项目中使用带有Ninject的Identity 2.2.1,并且除了UserManager之外,所有内容似乎都运行良好。 UserManager已创建,但是当它被注入服务时,它只包含它的默认实现,而不是它应该使用的配置。

我试图在这里遵循多种解决方案来解决问题,但无济于事。有人可以指出我出错的地方吗。

Startup.cs

public class Startup
{
    internal static IDataProtectionProvider DataProtectionProvider { get; private set; }

    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        var kernel = NinjectConfig.CreateKernel();

        app.UseNinjectMiddleware(() => kernel);
        WebApiConfig.Register(config);
        ConfigureOAuth(app, kernel);
        app.UseNinjectWebApi(config);
        app.UseWebApi(config);
    }

    private void ConfigureOAuth(IAppBuilder app, IKernel kernel)
    {
        DataProtectionProvider = app.GetDataProtectionProvider();

        app.CreatePerOwinContext<UserManager>((option, context) =>
        {
            return kernel.Get<UserManager>();
        });

        app.UseCors(CorsOptions.AllowAll);

        app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/token"),
            Provider = new AuthorizationServerProvider(),
            AccessTokenExpireTimeSpan = TimeSpan.FromHours(6),
            AllowInsecureHttp = true    // TODO: Change when in production
        });

        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }
}

NinjectConfig.cs

public static class NinjectConfig
{
    public static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());

        RegisterServices(kernel);

        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
    }
}

IdentityModule.cs

public class IdentityModule : NinjectModule
{
    public override void Load()
    {
        Bind<IUserStore<User>>().To<UserStore<User>>().WithConstructorArgument("context", new NeedsCollectorDbContext());
        Bind<UserManager>().ToSelf();
    }
}

UserManager.cs

public class UserManager : UserManager<User>
{
    public UserManager(IUserStore<User> store) : base(store)
    {
        UserValidator = new UserValidator<User>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        var dataProtectionProvider = Startup.DataProtectionProvider;
        if (dataProtectionProvider != null)
        {
            UserTokenProvider = new DataProtectorTokenProvider<User>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
    }
}

UserManager在Startup.cs中使用正确的配置进行实例化,但是当稍后注入服务时它会丢失,请参阅屏幕截图 - 注入时UserTokenProvider为null。

Startup.cs

Within a service

由于

0 个答案:

没有答案