【问题标题】:SignalR CryptographicException on AzureWebsitesAzureWebsites 上的 SignalR CryptographicException
【发布时间】:2013-03-01 21:09:56
【问题描述】:

我在使用 SignalR 时遇到了这个异常,部署在 Azure 网站中。它在调试环境中运行良好。它是 SignalR 1.0.1,我使用 .NET MVC 和 WebApi

The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Security.Cryptography.CryptographicException: The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 
[CryptographicException: The data protection operation was unsuccessful. This may have     been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating.]
Microsoft.Owin.Host.SystemWeb.<>c__DisplayClass1.<GetRethrowWithNoStackLossDelegate>b__0(Exception ex) +27
Microsoft.Owin.Host.SystemWeb.Utils.RethrowWithOriginalStack(Exception ex) +15
Microsoft.Owin.Host.SystemWeb.CallContextAsyncResult.End(IAsyncResult result) +47
Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.EndProcessRequest(IAsyncResult result) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

你有什么想法吗? 谢谢

【问题讨论】:

  • 我猜您正在使用自定义依赖解析器,并且您没有注册正确的 IProtectedData,或者您在不应该注册的时候实现了注册。
  • 我正在使用 Ninject,并且没有注册 IProtectedData。我是不是该 ?怎么样?
  • 没关系,我去 Azure Cloud,谢谢
  • 确定吗?我不确定“Azure Cloud”与这个问题有什么关系。无论如何,你做错了什么。您是否覆盖了注册?如果你这样做了,请不要这样做,它可能会在 azure 网站上运行。
  • 我不知道如何,但它可以与 Azure 云计算一起使用。我没有覆盖注册。

标签: azure asp.net-web-api signalr azure-web-app-service signalr-hub


【解决方案1】:

我不相信 Azure 网站能够与证书/加密 API 进行通信。尝试调用 Azure 管理 API 时会出现类似问题。运行站点的用户上下文似乎没有足够的权限来执行此操作。

【讨论】:

    【解决方案2】:

    这是允许我使用以下代码解决相同问题的单个帖子。 dfowlers 提到注册 IProtectedData 的实例,这让我搜索并找到了定义 here

    请注意,使用 Visual Studio 开发服务器时未遇到此问题,但在上线时未遇到此问题。我很高兴我找到了这篇文章,因为我不知道我应该如何知道要实现 IProtectedData。也许文档中有更深的内容。

    我不确定这是否是 100% 正确的解决方案,但这对我有用。我创建了一个实现 IProtectedData 的类,然后用 Ninject 注册了它。

    类:

    using Microsoft.AspNet.SignalR.Infrastructure;
    
    namespace Fwr.DataTeamUploader.Logic
    {
        public class ProtectedData : IProtectedData
        {
    
            // Obviously this isn't doing much to protect the data,
            // assume custom encryption required here
    
            // To reiterate, no encryption is VERY^4 BAD, see comments.
    
            public string Protect(string data, string purpose)
            {
                return data;
            }
    
            public string Unprotect(string protectedValue, string purpose)
            {
                return protectedValue;
            }
        }
    }
    

    Ninject 注册:

    /// <summary>
    /// Load your modules or register your services here
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        ...
    
        kernel.Bind<IProtectedData>().To<ProtectedData>();
    
        ...
    

    【讨论】:

    • 我不会显示这样的代码,因为当您删除加密时。这是非常非常非常非常糟糕的,因为现在你完全没有受到保护。我知道您提到您应该在此处使用自定义加密,但即使使用此代码显示它也很糟糕。发生此问题的原因是由于某些奇怪的原因,它使用了错误的数据保护。它应该使用在依赖解析器中调用 MapHubs pts 的机器密钥提供程序。
    • 感谢 dfowler。我知道这意味着通讯按照注释是不安全的。这篇文章只是以最简单的方式演示了解决方法。从在 Win 7 机器上使用 SignalR 1.0.1 的开发迁移到 Win Server 2008 R2 生产服务器时,会出现此错误。由于没有关于问题来源的其他信息,这是我修复它的唯一方法(使用自定义加密)。你能建议一个解决问题的更好方法吗?我会相应地编辑帖子。
    • 我同意@dfowler 的观点,但我在 DEV 环境中遇到了同样的问题(Win 7 - VS 2010 - Unity IoC),这是迄今为止我发现的唯一解决方案。欢迎提出任何建议!
    • 您的 Unity IoC 可能是罪魁祸首。您可能会错误地将调用 MapHubs 时注册的受保护数据替换为默认数据。调用 MapHubs() 会注册一个基于机器密钥的受保护数据实现,而不是在 Windows azure 上引发此异常的默认实现。如果你能弄清楚它被替换的原因,那么你就会找出问题所在。
    【解决方案3】:

    现在在这种情况下,您现在可以使用来自Microsoft.AspNet.SignalR.SystemWeb 包的扩展方法MapsHubs()

    MachineKeyProtectedData 将被使用,而不是默认实现。

    【讨论】:

      【解决方案4】:

      对于访问此页面的其他人,我遇到了同样的问题,但解决方案要简单得多。正如上面的 cmets 所述,接受的答案很糟糕。还提到的是,SignalR 默认使用MachineKeyDataProtector 代替IProtectedDataMapHubsMapConnection 都调用一个函数 InitializeProtectedData,该函数将 MachineKeyDataProtector 注册到依赖解析器。

      我的问题是我正在映射我的 SignalR 路由,然后配置依赖解析器

      RouteTable.Routes.MapConnection<SomeEndpoint>("SomeEndpoint", "SomeEndpointUrl");
      GlobalHost.DependencyResolver = 
                           new StructureMapDependencyResolver(ObjectFactory.Container);
      

      所以基本上由 MapConnection 完成的 IProtectedData 解析器注册 -> 当我注册我的自定义解析器时,InitializeProtectedData 被吹走了。简单的修复,在映射连接之前设置解析器。

      GlobalHost.DependencyResolver = 
                           new StructureMapDependencyResolver(ObjectFactory.Container);
      RouteTable.Routes.MapConnection<SomeEndpoint>("SomeEndpoint", "SomeEndpointUrl");
      

      【讨论】:

        猜你喜欢
        • 2020-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多