【问题标题】:Error occurred during a cryptographic operation in debug调试中的加密操作期间发生错误
【发布时间】:2016-04-11 13:08:32
【问题描述】:

我收到以下错误。这似乎是在我升级我的 Visual Studio 2015 以进行第一次更新后才开始的。我在这里阅读了一些关于这与机器密钥有关的问题?我不知道如何解决它并防止它。目前,当我使用 IIS express 在调试中运行此程序时,我在本地计算机上遇到此错误。

Exception Details: System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation.

Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId);

// place the entry in memory
this.Deserialize((Cache == null) ? null : MachineKey.Unprotect(Cache.cacheBits,"ADALCache"));


[CryptographicException: Error occurred during a cryptographic operation.]
   System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(Func`2 func, Byte[] input) +115
   System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.Unprotect(Byte[] protectedData) +70
   System.Web.Security.MachineKey.Unprotect(ICryptoServiceProvider cryptoServiceProvider, Byte[] protectedData, String[] purposes) +62
   System.Web.Security.MachineKey.Unprotect(Byte[] protectedData, String[] purposes) +121
   LEDES.Models.ADALTokenCache..ctor(String signedInUserId) in C:\Users\RLewis\Source\Workspaces\Workspace\LEDES\LEDES\Models\AdalTokenCache.cs:28
   LEDES.Startup.<ConfigureAuth>b__7_0(AuthorizationCodeReceivedNotification context) in C:\Users\RLewis\Source\Workspaces\Workspace\LEDES\LEDES\App_Start\Startup.Auth.cs:54
   Microsoft.Owin.Security.OpenIdConnect.<AuthenticateCoreAsync>d__1a.MoveNext() +4388
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26
   Microsoft.Owin.Security.OpenIdConnect.<AuthenticateCoreAsync>d__1a.MoveNext() +5776
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28
   Microsoft.Owin.Security.Infrastructure.<BaseInitializeAsync>d__0.MoveNext() +471
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +218
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +170
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +525
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +170
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<DoFinalWork>d__2.MoveNext() +166
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +26
   Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +26
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +81
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +30
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +380
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

这是失败的代码 - 这是由 VS 在设置项目时选择 Azure 身份验证时生成的。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Security;
using Microsoft.IdentityModel.Clients.ActiveDirectory;

namespace LEDES.Models
{
    public class ADALTokenCache : TokenCache
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        private string userId;
        private UserTokenCache Cache;

        public ADALTokenCache(string signedInUserId)
        {
            // associate the cache to the current user of the web app
            userId = signedInUserId;
            this.AfterAccess = AfterAccessNotification;
            this.BeforeAccess = BeforeAccessNotification;
            this.BeforeWrite = BeforeWriteNotification;
            // look up the entry in the database
            Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId);
            // place the entry in memory
            this.Deserialize((Cache == null) ? null : MachineKey.Unprotect(Cache.cacheBits,"ADALCache"));
        }

        // clean up the database
        public override void Clear()
        {
            base.Clear();
            var cacheEntry = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId);
            db.UserTokenCacheList.Remove(cacheEntry);
            db.SaveChanges();
        }

        // Notification raised before ADAL accesses the cache.
        // This is your chance to update the in-memory copy from the DB, if the in-memory version is stale
        void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            if (Cache == null)
            {
                // first time access
                Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId);
            }
            else
            { 
                // retrieve last write from the DB
                var status = from e in db.UserTokenCacheList
                             where (e.webUserUniqueId == userId)
                select new
                {
                    LastWrite = e.LastWrite
                };

                // if the in-memory copy is older than the persistent copy
                if (status.First().LastWrite > Cache.LastWrite)
                {
                    // read from from storage, update in-memory copy
                    Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId);
                }
            }
            this.Deserialize((Cache == null) ? null : MachineKey.Unprotect(Cache.cacheBits, "ADALCache"));
        }

        // Notification raised after ADAL accessed the cache.
        // If the HasStateChanged flag is set, ADAL changed the content of the cache
        void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            // if state changed
            if (this.HasStateChanged)
            {
                Cache = new UserTokenCache
                {
                    webUserUniqueId = userId,
                    cacheBits = MachineKey.Protect(this.Serialize(), "ADALCache"),
                    LastWrite = DateTime.Now
                };
                // update the DB and the lastwrite 
                db.Entry(Cache).State = Cache.UserTokenCacheId == 0 ? EntityState.Added : EntityState.Modified;
                db.SaveChanges();
                this.HasStateChanged = false;
            }
        }

        void BeforeWriteNotification(TokenCacheNotificationArgs args)
        {
            // if you want to ensure that no concurrent write take place, use this notification to place a lock on the entry
        }

        public override void DeleteItem(TokenCacheItem item)
        {
            base.DeleteItem(item);
        }
    }
}

来自调用堆栈的信息和发生 CryptographicEsception 的点

    System.Web.dll!System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(System.Func<byte[], byte[]> func, byte[] input)   Unknown
    System.Web.dll!System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.Unprotect(byte[] protectedData)    Unknown
    System.Web.dll!System.Web.Security.MachineKey.Unprotect(System.Web.Security.Cryptography.ICryptoServiceProvider cryptoServiceProvider, byte[] protectedData, string[] purposes) Unknown
    System.Web.dll!System.Web.Security.MachineKey.Unprotect(byte[] protectedData, string[] purposes)    Unknown
>   LEDES.dll!LEDES.Models.ADALTokenCache.ADALTokenCache(string signedInUserId) Line 28 C#
    LEDES.dll!LEDES.Startup.ConfigureAuth.AnonymousMethod__7_0(Microsoft.Owin.Security.Notifications.AuthorizationCodeReceivedNotification context) Line 54 C#
    Microsoft.Owin.Security.OpenIdConnect.dll!Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationHandler.AuthenticateCoreAsync()  Unknown
    mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)  Unknown
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
    mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.InvokeAction(object state)    Unknown
    mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunCallback(System.Threading.ContextCallback callback, object state, ref System.Threading.Tasks.Task currentTask) Unknown
    mscorlib.dll!System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.Run(System.Threading.Tasks.Task task, bool canInlineContinuationTask)   Unknown
    mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
    mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree() Unknown
    mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)    Unknown
    mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Microsoft.Owin.IFormCollection>.SetResult(Microsoft.Owin.IFormCollection result)    Unknown
    Microsoft.Owin.dll!Microsoft.Owin.OwinRequest.ReadFormAsync()   Unknown
    mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)  Unknown
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
    mscorlib.dll!System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation..cctor.AnonymousMethod__8_0(object state)   Unknown
    System.Web.dll!System.Web.AspNetSynchronizationContext.Post.AnonymousMethod__0()    Unknown
    System.Web.dll!System.Web.Util.SynchronizationHelper.SafeWrapCallback(System.Action action) Unknown
    System.Web.dll!System.Web.Util.SynchronizationHelper.QueueAsynchronous.AnonymousMethod__0(System.Threading.Tasks.Task _)    Unknown
    mscorlib.dll!System.Threading.Tasks.ContinuationTaskFromTask.InnerInvoke()  Unknown
    mscorlib.dll!System.Threading.Tasks.Task.Execute()  Unknown
    mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
    mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot)    Unknown
    mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Unknown
    mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() Unknown
    mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()    Unknown
    mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Unknown
    [Native to Managed Transition]  

【问题讨论】:

  • 正确的堆栈跟踪将有助于理解您的问题。
  • 你能检查一下那个线程吗:blogs.msdn.com/b/webdev/archive/2012/10/23/… .config 文件中有一整节关于与 machineKey 设置的兼容性。
  • 谢谢,我去看看。我不明白为什么突然停止工作。如果在生产中发生这种情况,我会担心
  • 如果更新以某种方式破坏了您的 iis express 证书/密钥(这可能是您问题的根源),请尝试转到添加/删除程序并选择 IIS Express 上的“修复”选项。这样证书将重新安装。
  • 它什么都没有。修复了visual studio和iis,同样的问题

标签: c#


【解决方案1】:

遇到了同样的问题。在“使用它”一个多小时后,我进入成员数据库(通常由 Visual Studio 自动创建)并从 UserTokenCaches 表中删除所有行。运行应用程序,通过密码错误消息。一条新的缓存令牌记录已创建并插入到表中。

【讨论】:

  • 我也做了同样的事情......正在考虑发布答案......但它已经存在了。 ...任何方式只需清除您的表“UserTokenCaches”即可解决您的问题
  • 这对我有用,但我不知道为什么。 Azure 网站
  • 这是最直接的解决方法...为什么会发生这种情况?
  • 这只是暂时解决了这个问题。机器配置缺少机器密钥。将其添加到 webconfig 解决了我的问题
【解决方案2】:

添加堆栈跟踪

你没有。了解为什么会收到如此无用的异常消息非常重要。这是有意的。 System.Web 隐藏了密码失败的真正原因。您会收到一条乏味的错误消息(“它不起作用”),并且没有实际失败代码的堆栈跟踪。

很重要,因为不这样做是危险,它允许攻击者使用故意格式错误的数据来探测您的网络应用程序,并从异常中获取知识以找到破解您的安全代码的方法。

您需要获得更好的堆栈跟踪和异常消息才能找到真正的原因。这需要您告诉调试器在抛出异常时停止。真正的,而不是平淡的。在 VS2015 中,使用调试 > Windows > 异常设置。单击“Common Language Runtime Exceptions”复选框,使其从矩形变为复选标记。另外:工具>选项>调试>常规>取消选中“仅启用我的代码”复选框。

【讨论】:

  • 谢谢,这给了我另一个错误,与您评论过的相同 - stackoverflow.com/questions/21985217/… 它通过返回上面的错误并显示相同的“堆栈跟踪”信息来完成更多。有点迷失了下一步该做什么
  • 这不是错误。使用 Debug > Windows > Call Stack 获取堆栈跟踪。异常消息在“输出”窗口中可见。
  • 从调用堆栈添加信息
【解决方案3】:

找到了解决办法。

错误来自的代码部分是 AdalToken.Cache.cs 文件。

userId = signedInUserId;
this.AfterAccess = AfterAccessNotification;
this.BeforeAccess = BeforeAccessNotification;
this.BeforeWrite = BeforeWriteNotification;
// look up the entry in the database
Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == userId);
// place the entry in memory
this.Deserialize((Cache == null) ? null : MachineKey.Unprotect(Cache.cacheBits,"ADALCache"));

特别是最后一行。

同样相关的是 db.UserTokenCacheList 的上下文:

{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserTokenCache> UserTokenCacheList { get; set; }
    }

    public class UserTokenCache
    {
        [Key]
        public int UserTokenCacheId { get; set; }
        public string webUserUniqueId { get; set; }
        public byte[] cacheBits { get; set; }
        public DateTime LastWrite { get; set; }
    }
}

所有这些都是在我开始这个新项目时通过设置 Azure 身份验证的向导时由 Visual Studio 生成的。

关于ApplicationDbContext中的base("DefaultConnection")

在我的 web.config 中没有这方面的条目,但是直到最近这一直有效。

在 web.config 的 中,我为 DefaultConnection 添加了一行以指向我的数据库,现在一切正常,至少现在是这样。

希望这对遇到同样错误的人有所帮助。

【讨论】:

    【解决方案4】:

    MachineKey.Unprotect 方法的文档解释了 CryptographicException 的原因,VS 的 Intellisense 也提供了该方法:

    “可能的原因包括:应用程序部署到多个服务器并且使用自动生成的加密密钥。” (强调)

    当您的现有会话 cookie 与前一个会话具有相同的会话 ID,但服务器使用不同的密钥进行加密操作时,会发生此错误。 ADALTokenCache 正在使用对称加密对会话进行加密/解密,这需要双向相同的密钥

    UserTokenCaches表中检索会话数据如下:

    1. 获取用户 ID(例如从声明中)
    2. 检索cacheBits 其中webUserUniqueID = 用户ID
    3. 使用MachineKey作为解密密钥解密cacheBits

    当 MachineKey 自首次加密后发生更改时,解密失败并引发 CryptographicException。您可以通过复制以下两个对称加密示例来创建类似的情况:Encrypting DataDecrypting Data,但更改其中一个密钥。

    由于某种原因,当您更新 Visual Studio 时,它会重新生成您的 MachineKey。由于您的 Web 浏览器仍然具有相同的会话 cookie,因此 ASP.NET 从数据库中检索了您之前的会话,但无法再对其进行解密。所有其他答案通过阻止AdalTokenCache 能够检索上一个会话来解决本地调试问题,但并没有真正解决根本原因(当然对于生产系统不可行!)

    • 清除 cookie 有效,因为 ASP.NET 必须创建一个新会话,而不是检索您以前的会话。这很难与生产系统的用户沟通。
    • UserTokenCaches 数据库表中删除所有行的工作原理相同 - 它无法检索上一个会话,而必须创建一个新会话。这将结束您应用程序的所有生产实例中的所有用户会话,并且您不会获得维护窗口来执行此操作,因为您的一些用户会遇到异常。

    更好的解决方案是确保生产部署都使用相同的加密密钥(例如,在 web.config 中设置 MachineKey),或者以不同的方式保护对会话数据的访问。有些人通过将加密密钥与加密数据存储在同一个数据库中来解决这个问题,但我真的不明白这样做的意义......

    【讨论】:

      【解决方案5】:

      请检查您的机器配置文件是否有机器密钥配置。如果没有,则在您的 web.config 中添加机器密钥配置。

      <system.web>    
          <machineKey
            validationKey="your validationKey"
            decryptionKey="your decryptionKey"      
          />
      </system.web>
      

      这解决了我的问题

      【讨论】:

      • 如果你已经有机器密钥,我必须在更新 AD 组件后重新生成我的。
      【解决方案6】:

      这些答案都不适合我。就我而言,清除浏览器的历史记录解决了这个问题。或许,只需删除 cookie 就足够了。

      【讨论】:

        猜你喜欢
        • 2014-04-21
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 2013-03-28
        • 2016-02-22
        • 2019-07-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多