มีข้อยกเว้นเกิดขึ้นในขณะที่แยกโทเค็นออกไม่สามารถถอดรหัสโทเค็นป้องกันการปลอมแปลงในแอปพลิเคชัน. Net Core 2.2 ได้

Aug 17 2020

ฉันได้รับข้อผิดพลาดในบันทึกของฉัน ฉันใช้เวลาเกือบทั้งวันในการค้นหาวิธีแก้ปัญหา แต่ไม่พบวิธีที่ตรงกับความต้องการของฉัน

นี่คือข้อผิดพลาดในการบันทึก

ความรุนแรง = [ERROR], ipaddress = xxxx, subprocess = Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery, description = มีข้อยกเว้นเกิดขึ้นขณะที่แยกโทเค็นออก Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: ไม่สามารถถอดรหัสโทเค็นป้องกันการปลอมแปลงได้ ---> System.Security.Cryptography.CryptographicException: ไม่พบคีย์ {xxxxxxxxxx} ในคีย์ริง ที่ Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore (Byte [] protectedData บูลีน allowOperationsOnRevokedKeys, UnprotectStatus และสถานะ) ที่ Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect (Byte [] protectedData บูลีน ignoreRevocationErrors บูลีนและ requiresMigration บูลีนและ wasRevoked ) ที่ Microsoft.AspNetCore.DataProtection.KeyManagementKeyRingBasedDataProtector.Unprotect (Byte [] protectedData) ที่ Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgeryTokenSerializer.Deserialize (String serializedToken) ที่ Microsoft.AspNetCore.Antiforgizer.Internal.DefaultAntiforgeryToken DefaultAntiforgery.GetCookieTokenDoesNotThrow (HttpContext httpContext)

    "Certificates": {
    "StoreName": "My",
    "StoreLocation": "LocalMachine"
    "SerialNumber": "xxxxxxxxxxxx"
},
   
   private X509Certificate2 LCertificate()
    {
        var storeName = Configuration["Certificates:StoreName"];
        var storeLocation = Configuration["Certificates:StoreLocation"];
        string serialNumber = Configuration["Certificates: SerialNumber"];
        using(X509Store store = new X509Store(storeName,storeLocation))
        {
            var certificates = store.Certificates
                                    .Find(X509FindType.FindBySerialNumber,
                                          serialNumber,
                                          acceptValidCertOnly);             

            return certificates[0];
        }
    }
    
     public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentityServer
                .AddSigningCredential(new X509Certificate2(LCertificate()))
      
    }

   [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginModel model)
    {

คำตอบ

4 RoarS. Aug 17 2020 at 10:25

ถ้า

  • แอปของคุณโฮสต์บนเซิร์ฟเวอร์หลายเครื่อง
  • ไม่ได้กำหนดค่าการป้องกันข้อมูลที่ใช้ร่วมกัน
  • คุณไม่ได้ใช้เซสชันติดหนึบ

สิ่งนี้จะเกิดขึ้นเมื่อผู้ใช้ร้องขอเพจที่มีแบบฟอร์มจากเซิร์ฟเวอร์ A และส่งแบบฟอร์มไปยังเซิร์ฟเวอร์ B ในภายหลัง

นอกจากนี้ยังอาจเกิดขึ้นบนเซิร์ฟเวอร์ IIS เดียวหาก

  • ผู้ใช้ร้องขอเพจที่มีแบบฟอร์ม
  • คุณรีสตาร์ทเซิร์ฟเวอร์
  • ผู้ใช้ส่งแบบฟอร์ม

เหตุผลก็คือการรีสตาร์ททำให้คีย์ริงใหม่โหลดลงในหน่วยความจำและคีย์ป้องกันการเสื่อมสภาพภายในแบบฟอร์มไม่สามารถตรวจสอบความถูกต้องได้อีกต่อไป

กรณีหลังนี้สามารถแก้ไขได้ใน IIS โดยการตรวจสอบ "โหลดโปรไฟล์ผู้ใช้" ในแอปพูล

ข้อมูลเพิ่มเติม: https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-3.1