2015-07-20 128 views
1

我正在適應ASP身份驗證功能從WCF Web服務調用。我使用ninject進行跨項目的依賴項注入,所以我有一個自定義類(UserProfilManager)用於實現標識函數,方法是注入我的構造函數所需的依賴項。依賴注入'SignInManager'(身份2.0)與Ninject

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的程序集。 - 找不到包含啓動或[AssemblyName] .Startup類的程序集。 要禁用OWIN啓動發現,請在web.config中添加值爲「false」的appSetting owin:AutomaticAppStartup。 要指定OWIN啓動程序集,類或方法,請在web.config中將appSetting owin:AppStartup與完全限定的啓動類或配置方法名稱相加。

我沒有啓動類,我並不需要它,我試圖從我的WCF的web.config添加<add key="owin:AutomaticAppStartup" value="false" />搜索該類全髖關節,但它拋出一個內部服務器錯誤: 失敗添加服務。服務的元數據可能不可用。確保您的服務正在運行並顯示元數據。

有人可以告訴我我做錯了什麼,或者建議一個基於我的代碼的解決方案。

回答

2

您在這裏看到的是OWIN試圖圍繞Web服務器啓動自己。但是因爲您運行WCF,它無法執行任何操作。 Bascially OWIN說它不能在當前配置的WCF中運行。 IAuthenticationManager在HTTP請求上設置cookie,但由於WCF不僅是HTTP,所以OWIN的cookie不能應用。

因此,您需要檢查您的體系結構WCF服務如何進行身份驗證以及它的進展情況。

看在other questions and answers,我不think you can makeOWIN work with WCFBut you can try...

+0

您是否知道在WCF運行時如何配置OWIN?在一個簡單的Web應用程序中,有一個啓動類調用一個方法配置(IAppBuilder應用程序)來完成這項工作。你認爲我可以在WCF中做同樣的事嗎? – Idham

+0

我不認爲你可以在OWIN中使用WCF。請參閱編輯鏈接到其他類似的問題。 – trailmax