在我的MVC應用程序中,我使用表單身份驗證來驗證用戶,然後使用System.IdentityModel.Services.SessionAuthenticationModule
來保持會話。MachineKeySessionSecurityTokenHandler和會話令牌在應用程序重新啓動之間到期
雖然我還沒有達到必要的程度,但我確實想要使用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設置,它只是幫助我解決了這個問題。 Mike –
@indiecodemonkey - 我很高興這對你有幫助! –
只是認爲如果您使用SessionAuthenticationModule在不使用表單身份驗證的情況下對用戶進行身份驗證,則需要通過此過程實現共享WIF緩存(例如,不會在應用程序池重置時重置)。這是一個SessionSecurityTokenCache。在MSDN上的一個例子是https://msdn.microsoft.com/en-us/library/hh545457(v=vs.110).aspx –