简体   繁体   English

Web场中的TempData失败

[英]TempData fails in a web farm

When deployed to a web farm (basically 2 Windows Server 2016 boxes), the code below fails to read data from TempData. 当部署到Web场(基本上是2个Windows Server 2016框)时,下面的代码无法从TempData读取数据。 The sequence of events is as follows: UserInfo page is POSTed to, TempData is set, then redirects to the GET of UserInfo. 事件的顺序如下:将UserInfo页面发布到,设置TempData,然后重定向到UserInfo的GET。 In the GET TempData can't be read. 在GET中,无法读取TempData。

[HttpPost]
public IActionResult UserInfo(Model model) {
    TempData["Model"] = JsonConvert.SerializeObject(model);
    return RedirectToAction("UserInfo");            
}

[HttpGet]
public IActionResult UserInfo() {
    string serialized = (string)TempData["Model"];
    if (serialized != null) {
        var model = JsonConvert.DeserializeObject<Model>(serialized);
        return View(model);
    } else {
        // nothing here redirect home
        return RedirectToAction("Landing");
    }
}

I get the following error in the std log (but doesn't cause an exception in my code): 我在标准日志中收到以下错误(但在我的代码中未引起异常):

warn: Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider[3]
      The temp data cookie .AspNetCore.Mvc.CookieTempDataProvider could not be loaded.

System.Security.Cryptography.CryptographicException: The key {0dd9c024-af79-407b-9820-db7f094975f9} was not found in the key ring.
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
   at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
   at Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider.LoadTempData(HttpContext context)

PS I should mention that this code works in non-web farm IIS instances and on Asure shared hosting. PS我应该提到,此代码可在非Web场IIS实例和Asure共享主机上使用。

The exception tells you everything you need to know. 异常告诉您所有您需要了解的内容。 It attempted to read the cookie, but decryption failed, specifically because the key could not be found in the data protection key ring. 它尝试读取cookie,但是解密失败,特别是因为在数据保护密钥环中找不到该密钥。 That means you're not sharing data protection between each instance in the farm. 这意味着您不会在服务器场中的每个实例之间共享数据保护。 Even though it's the same app, each instance is effectively it's own entity. 即使它是同一个应用程序,每个实例实际上都是它自己的实体。 As such, you need to treat it like any other scenario where data protection needs to be shared between sites: namely by using a common key ring store and ensuring that the same application name is used: 因此,您需要像需要在站点之间共享数据保护的任何其他方案一样对待它:即,通过使用公共密钥环存储并确保使用相同的应用程序名称:

services.AddDatProtection()
    .SetApplicationName("Shared Name")
    .PersistKeysToFileSystem(new DirectoryInfo(@"\\server\share\directory\"));

With that, each instance will be able to read the cookies set by other instances, which then will make TempData work between instances. 这样,每个实例将能够读取其他实例设置的cookie,这将使TempData在实例之间工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM