【发布时间】:2013-04-12 21:27:10
【问题描述】:
在我的 MVC 应用程序中,我使用表单身份验证对用户进行身份验证,然后使用System.IdentityModel.Services.SessionAuthenticationModule 来保持会话。
虽然我还没有到必要的地步,但我确实想使用 System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler 以便应用程序可以在网络农场上很好地运行(如 Dominick Baier here 所述)。
我遇到的问题是,考虑到基于 machineKey 的处理,我希望会话不仅在服务器机器之间有效,而且应该在应用程序重新启动后仍然有效。但是,每当我重新启动或重建应用程序时,在浏览器中点击应用程序时,cookie 显然会变得无效,并且我会被弹回身份验证屏幕。一旦再次通过身份验证,一切都很好,会话仍然存在。但是,下次重新启动或重建应用程序时,我不得不重新进行身份验证。
我确定这是我没有得到的 WIF 的一个方面,但我只是不知道从这里转向哪里。我不怕扩展MachineKeySessionSecurityTokenHandler,但我想确保在继续之前了解这里发生了什么。我知道默认的 SessionSecurityTokenHandler 使用 DPAPI 结合应用程序池中的一些标识符进行加密,所以在这种情况下会发生这种情况是有道理的,但 MachineKeySessionSecurityTokenHandler 中的行为让我感到困惑。在MachineKeySessionSecurityTokenHandler 所依赖的重新启动时重新创建的应用程序中是否还有一些标识符?我只是缺少一个设置吗?
以下是我的 web.config 中的相关部分:
<configSections>
<section name="system.identityModel"
type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</configSections>
...
<system.identityModel>
<identityConfiguration>
<securityTokenHandlers>
<remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</securityTokenHandlers>
</identityConfiguration>
</system.identityModel>
...
<system.web>
<machineKey compatibilityMode="Framework45"
validationKey="E27893..."
decryptionKey="ABC..."
validation="SHA1" decryption="AES" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login"
timeout="10080" />
</authentication>
</system.web>
...
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="SessionAuthenticationModule"
type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</modules>
</system.webServer>
【问题讨论】:
-
克里斯,只是想对你说声谢谢。我一直在努力让这样的东西为我现在正在做的事情工作,而我遇到的问题是让我的 web.configs 中的值正确。因此,感谢您发布 web.config 设置,b/c 它只是帮助我解决了这个问题。迈克
-
@indiecodemonkey - 很高兴这对您有所帮助!
-
只是想我会说,如果您使用 SessionAuthenticationModule 来验证用户而不使用表单身份验证,则需要最后一块来实现共享 WIF 缓存(例如,不会在应用程序池重置时重置)。那是一个 SessionSecurityTokenCache。 MSDN 上的一个例子在这里msdn.microsoft.com/en-us/library/hh545457(v=vs.110).aspx