使用Ninject注入“SignInManager”(Identity 2.0)的依赖项

时间:2015-07-20 16:10:55

标签: c# wcf ninject asp.net-identity

我正在调整ASP身份验证功能,以便从WCF Web服务调用。

我在整个项目中使用ninject进行依赖注入,所以我有一个自定义类(UserProfilManager),我通过注入构造函数所需的依赖项来实现Identity函数。

public class UserProfilManager
{
    private readonly UserStore _userStore;
    private readonly UserManager<IdentityUser, Guid> _userManager;
    private readonly SignInManager<IdentityUser, Guid> _signInManager;

    public UserProfilManager(UserStore userStore, 
        UserManager<IdentityUser, Guid> userManager, 
        SignInManager<IdentityUser, Guid> signInManager)
    {
        _userStore = userStore;
        _userManager = userManager;
        _signInManager = signInManager;
    }

    //here I call Identity functionality (PasswordSignInAsync), GetByLogin is a function ment to be called from WCF
    public async Task<SignInStatus> GetByLogin(string login, string password, bool RememberMe, bool shouldLockout)
    {
        return await _signInManager.PasswordSignInAsync(login, password, RememberMe, shouldLockout);
    }

    //FindByNameAsync function works fine, UserStore binding : OK
    public User GetByName(string login)
    {
        var user = _userStore.FindByNameAsync(login);
        return new User
        {
            //...
        };
    }
}

'UserStore'和'UserManager'绑定完成,但'SignInManager'抛出ActivationException: 激活IAuthenticationManager时出错,没有匹配的绑定可用,且该类型不可自我绑定。 我通过添加以下内容来修复它:

public class UserModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IDbContext>().To<DbContext>();

            Bind<IUserProfilManager>().To<UserProfilManager>();
            Bind<IUserStore<IdentityUser, Guid>>().To<UserStore>();

            Bind<IAuthenticationManager>().
                ToMethod(c => HttpContext.Current.GetOwinContext().Authentication).InRequestScope();

        }
    }

现在它给了我这个错误:

尝试加载应用时发生以下错误。 - 找不到包含OwinStartupAttribute的程序集。 - 找不到包含Startup或[AssemblyName] .Startup类的程序集。 要禁用OWIN启动发现,请在web.config中添加值为“false”的appSetting owin:AutomaticAppStartup。 要指定OWIN启动程序集,类或方法,请在web.config中添加appSetting owin:AppStartup以及完全限定的启动类或配置方法名称。

我没有启动类而且我不需要它,我试图通过在我的WCF web.config中添加<add key="owin:AutomaticAppStartup" value="false" />来搜索该类,但它会引发内部服务器错误: 无法添加服务。服务的元数据可能不可用。确保您的服务正在运行并公开元数据。

有人可以告诉我我做错了什么,或根据我的代码建议解决方案。

1 个答案:

答案 0 :(得分:2)

你在这里看到的是OWIN试图围绕网络服务器开始。但是因为你运行WCF它不能做任何事情。基本上OWIN表示它无法在当前配置中在WCF中运行。 IAuthenticationManager在HTTP请求上设置cookie,但由于WCF不仅是HTTP,因此无法应用OWIN的cookie。

因此,您需要检查您的体系结构WCF服务的身份验证方式及其进展情况。

关注other questions and answers,我不think you can make OWIN work with WCFBut you can try...