【发布时间】: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 回答 8937123 和 8926099,我还添加了以下内容:
<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