2012-10-17 91 views
4

我正在嘗試使用.NET 4.5,MVC 4和使用自定義服務器端登錄頁面的主動重定向來使用FederatedAuthentication,使用代碼this教程和this代碼示例。爲什麼FederatedAuthentication.WSFederationAuthenticationModule在MVC Azure ACS聯合身份驗證中爲空?

重定向到我的AccountController的登錄方法工作正常,方法如下:

public ActionResult LogOn() 
{ 
    HrdClient hrdClient = new HrdClient(); 
    WSFederationAuthenticationModule fam = FederatedAuthentication.WSFederationAuthenticationModule; /*** Fails here because this is null **/ 
    HrdRequest request = new HrdRequest(fam.Issuer, fam.Realm, context: Request.QueryString["ReturnUrl"]); 
    IEnumerable<HrdIdentityProvider> hrdIdentityProviders = hrdClient.GetHrdResponse(request); 
    ViewData["Providers"] = hrdIdentityProviders; 
    return View(); 
} 

失敗的原因FederatedAuthentication.WSFederationAuthenticationModule爲空。

使用VS 2012,我運行了新的標識和訪問嚮導(這似乎取代了舊的STS對話框)。這給了我一個FederationMetadata的文件夾,看起來正確,並對我的Web.Config進行了一些修改。特別是,模塊部分看起來是這樣的:

<system.webServer> 
<validation validateIntegratedModeConfiguration="false" /> 
<modules runAllManagedModulesForAllRequests="true"> 
    <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /> 
    <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /> 
</modules> 

,並具有見過這麼回答89371238926099,我已經添加了以下還有:

<httpModules> 
    <add name="WSFederationAuthenticationModule" type="Microsoft.IdentityModel.Web.WSFederationAuthenticationModule, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
</httpModules> 

最後我的NuGet packages config顯示了MVC應用程序正確引用的Microsoft.IdentityModel:

<packages> 
    <package id="Microsoft.IdentityModel" version="6.1.7600.16394" targetFramework="net45" /> 
</packages> 

我也在social.msdn上看到了this question,這似乎表明STS對話框確實需要運行。

任何人都可以解釋爲什麼FederatedAuthentication.WSFederationAuthenticationModule將爲空,我能做些什麼來阻止這種情況的發生?

回答

12

我自己設法解決了這個問題,由於在SO上有一些類似於這個問題的未解答的問題,我將留下問題併發布自己的答案。

問題是將MVC應用程序升級到.NET 4.5。大部分的WIF功能都保持不變(至少在表面上),但是這些類都移到了不同​​的程序集中。我固定我的問題以下遷移準則這裏:http://msdn.microsoft.com/en-us/library/jj157089.aspx

最重要的事情是消除對Microsoft.IdentityModel包(V 3.5.0)舊引用,並確保它們通過向System.IdentityModelSystem.IdentityModel.Services dll的類似提法所取代,它應該是4.0版本,來自GAC而不是外部軟件包。

我的步驟,修復:

  • 清理掉所有我已經加入到Web.config中,並用默認的MVC配置文件再次啓動垃圾。
  • 在VS中取出Microsoft.IdentityModel包和去基準的DLL
  • 運行的訪問和身份精靈2012
  • 複製從<system.webServer><modules><system.web><httpModules>
  • System.IdentityModel.Services.WSFederationAuthenticationModule引用添加<authentication mode="Forms"><forms loginUrl="~/Account/LogOn" /></authentication>
  • 編譯,測試,舞蹈小跳舞的樂趣......

而且這得到了原WIF3.5/MVC3 Code Sample工作under.NET 4.5

+0

哇,即使有一個全新的MVC 4.5應用程序,我莫名其妙地結束了對Microsoft.IdentityModel參考。還需要將中的模塊引用複製到 Jedidja

+2

另外,感謝您發佈答案!現在在網上搜索VS2012/4.5中WIF的工作原理並不簡單。 – Jedidja