为什么在MVC Azure ACS联合身份验证中FederatedAuthentication.WSFederationAuthenticationModule为null?

Jud*_*her 4 asp.net-mvc federated-identity wif .net-4.5

我正在尝试使用自定义服务器端登录页面,使用教程中的代码以及代码示例,将FederatedAuthentication与.NET 4.5,MVC 4和主动重定向结合在一起.

重定向到我的AccountController的LogOn方法工作正常,方法如下所示:

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();
}
Run Code Online (Sandbox Code Playgroud)

这失败,因为FederatedAuthentication.WSFederationAuthenticationModule是null.

使用VS 2012,我运行了新的Identity and Access向导(它似乎取代了旧的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>
Run Code Online (Sandbox Code Playgroud)

在看过SO答案89371238926099之后,我还添加了以下内容:

 <httpModules>
  <add name="WSFederationAuthenticationModule" type="Microsoft.IdentityModel.Web.WSFederationAuthenticationModule, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpModules>
Run Code Online (Sandbox Code Playgroud)

最后,我的nuget包配置显示了Microsoft.IdentityModel,它被MVC应用程序正确引用:

<packages>
  <package id="Microsoft.IdentityModel" version="6.1.7600.16394" targetFramework="net45" />
</packages>
Run Code Online (Sandbox Code Playgroud)

我也在social.msdn上看过这个问题,这似乎表明需要运行STS对话框.

任何人FederatedAuthentication.WSFederationAuthenticationModule都可以解释为什么会为空,我能做些什么来阻止这种情况发生?

Jud*_*her 13

我自己设法解决了这个问题,因为在SO上有一些与此类似的未回答的问题,我会留下问题并发布我自己的答案.

问题是将MVC应用程序升级到.NET 4.5.大多数WIF功能保持不变(至少在表面上),但是所有类都移动到不同的程序集.我按照迁移指南修复了我的问题:http://msdn.microsoft.com/en-us/library/jj157089.aspx

最重要的是删除对Microsoft.IdentityModel包的旧引用(v 3.5.0)并确保它们被类似的对dll System.IdentityModelSystem.IdentityModel.Servicesdll的引用所取代,这些引用应该是4.0版,并且来自GAC而不是外部包.

我的修复步骤是:

  • 清除我添加到Web.Config的所有垃圾,然后重新启动默认的MVC配置文件.
  • 删除Microsoft.IdentityModel包并取消引用dll
  • 在VS 2012中运行Access和Identity向导
  • 重复System.IdentityModel.Services.WSFederationAuthenticationModule从参考<system.webServer><modules><system.web><httpModules>
  • <authentication mode="Forms"><forms loginUrl="~/Account/LogOn" /></authentication>
  • 编译,测试,跳舞小喜悦的跳汰机......

这使得原始的WIF3.5/MVC3 代码示例在.NET 4.5下运行

  • 另外,谢谢你发布答案!! 现在在网上搜索VS2012/4.5 for WIF的工作原理并不简单. (2认同)