【问题标题】:Why would FederatedAuthentication.WSFederationAuthenticationModule be null in MVC Azure ACS Federated Authentication?为什么 FederatedAuthentication.WSFederationAuthenticationModule 在 MVC Azure ACS 联合身份验证中为空?
【发布时间】:2012-10-08 02:25:46
【问题描述】:

我正在尝试使用来自this 教程和this 代码示例的代码,使用自定义服务器端登录页面将 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();
}

这失败了,因为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>

看到 SO 回答 89371238926099,我还添加了以下内容:

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

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

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

我还在 social.msdn 上看到了this question,这似乎表明确实需要运行 STS 对话框。

谁能解释为什么FederatedAuthentication.WSFederationAuthenticationModule 会为空,我能做些什么来阻止这种情况发生?

【问题讨论】:

    标签: asp.net-mvc wif .net-4.5 federated-identity


    【解决方案1】:

    我自己设法解决了这个问题,由于在 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 配置文件重新开始。
    • 删除 Microsoft.IdentityModel 包并取消引用 dll
    • 在 VS 2012 中运行访问和身份向导
    • &lt;system.webServer&gt;&lt;modules&gt; 复制System.IdentityModel.Services.WSFederationAuthenticationModule 引用到&lt;system.web&gt;&lt;httpModules&gt;
    • 添加&lt;authentication mode="Forms"&gt;&lt;forms loginUrl="~/Account/LogOn" /&gt;&lt;/authentication&gt;
    • 编译、测试、跳起欢乐的小舞步......

    这让原来的 WIF3.5 / MVC3 Code Sample 在 .NET 4.5 下工作

    【讨论】:

    • 哇,即使有了全新的 MVC 4.5 应用程序,我最终还是引用了 Microsoft.IdentityModel。并且还需要将模块引用从 复制到
    • 另外,感谢您发布答案!现在在网上搜索 VS2012 / 4.5 for WIF 的工作原理一点也不简单。
    • 你有没有可能还在使用 web.config 并可以将其粘贴进去?
    • @ΩmegaMan 不,抱歉。不久前我们重构为 .NET Core,虽然我认为该文件在某处的备份中,但经过这段时间后,我不知道在哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2012-12-16
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2013-09-13
    相关资源
    最近更新 更多