AzureWeb上的信号加密异常

AzureWeb上的信号加密异常,azure,asp.net-web-api,signalr,azure-web-app-service,signalr-hub,Azure,Asp.net Web Api,Signalr,Azure Web App Service,Signalr Hub,我在Azure网站上部署的SignalR中遇到了这个异常。它在调试环境中运行良好。它是Signalr1.0.1,我使用.NETMVC和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 whe

我在Azure网站上部署的SignalR中遇到了这个异常。它在调试环境中运行良好。它是Signalr1.0.1,我使用.NETMVC和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
数据保护操作未成功。这可能是由于没有为当前线程的用户上下文加载用户配置文件造成的,这可能是线程正在模拟的情况。
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.Security.Cryptography.Cryptography异常:数据保护操作未成功。这可能是由于没有为当前线程的用户上下文加载用户配置文件造成的,这可能是线程正在模拟的情况。
源错误:
在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别。
堆栈跟踪:
[加密异常:数据保护操作未成功。这可能是由于没有为当前线程的用户上下文加载用户配置文件造成的,这可能是线程模拟时的情况。]
Microsoft.Owin.Host.SystemWeb.c_uuuuDisplayClass1.b_uuu0(异常示例)+27
Microsoft.Owin.Host.SystemWeb.Utils.RethrowWithOriginalStack(例外示例)+15
Microsoft.Owin.Host.SystemWeb.CallContextAsyncResult.End(IAsyncResult结果)+47
Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.EndProcessRequest(IAsyncResult结果)+7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+9629708
System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成)+155
你知道吗?
谢谢

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

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

注意:使用VisualStudio开发服务器时没有遇到此问题,但在迁移到live时遇到了此问题。我很高兴我找到了这篇文章,因为我不知道如果不是这样,我该如何实现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;
        }
    }
}
第九项注册:

/// <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>();

    ...
//
///在此处加载模块或注册服务
/// 
///内核。
私有静态无效注册服务(IKernel内核)
{
...
kernel.Bind().To();
...

现在,在本例中,您可以使用Microsoft.AspNet.signar.SystemWeb包中的扩展方法
MapsHubs()


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

对于其他来到本页的人,我也有同样的问题,但解决方案要简单得多。如上所述,公认的答案是错误的。还提到的是,Signal默认使用
MachineKeyDataProtector
进行
IProtectedData
MapHubs
MapConnection
都调用一个函数
InitializeProtectedData
,该函数向依赖项解析程序注册
MachineKeyDataProtector

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

RouteTable.Routes.MapConnection<SomeEndpoint>("SomeEndpoint", "SomeEndpointUrl");
GlobalHost.DependencyResolver = 
                     new StructureMapDependencyResolver(ObjectFactory.Container);
RouteTable.Routes.MapConnection(“SomeEndpoint”、“SomeEndpointUrl”);
GlobalHost.DependencyResolver=
新的StructureMappDependencyResolver(ObjectFactory.Container);
因此,基本上,当我注册自定义解析程序时,由MapConnection->InitializeProtectedData完成的IProtectedData解析程序注册被破坏了。简单的修复方法是,在映射连接之前设置解析程序

GlobalHost.DependencyResolver = 
                     new StructureMapDependencyResolver(ObjectFactory.Container);
RouteTable.Routes.MapConnection<SomeEndpoint>("SomeEndpoint", "SomeEndpointUrl");
GlobalHost.DependencyResolver=
新的StructureMappDependencyResolver(ObjectFactory.Container);
RouteTable.Routes.MapConnection(“SomeEndpoint”、“SomeEndpointUrl”);

我猜您使用的是自定义依赖项解析程序,但您没有注册正确的IProtectedData,或者您在不应该注册的时候实现了注册。我使用的是Ninject,但没有注册IProtectedData。我应该吗?怎么做?不管怎样,我将使用Azure Cloud,谢谢?我不确定什么是“Azure Cloud”与此问题有关。无论如何,你都做了错事。你是否覆盖了注册?如果你覆盖了,不要这样做,它可能会在azure网站上工作。我不知道如何做,但它与azure云计算一起工作。我没有覆盖注册。我不会显示这样的代码,因为当你删除加密时。这是非常糟糕的现在你完全没有保护。我知道你提到你应该在这里使用自定义加密,但即使用此代码显示它也很糟糕。发生此问题的原因是出于某种奇怪的原因,它使用了错误的数据保护。它应该使用机器密钥提供程序,对MapHubs的调用在依赖项解析中使用了该程序版本。谢谢dfowler。我理解这意味着根据notes,通信是不安全的。这篇文章只是以最简单的方式演示了解决方法。当在Win 7机器上使用SignalR 1.0.1开发到Win Server 2008 R2生产服务器时,出现了此错误。没有关于问题来源的其他信息,这是我唯一的问题你能建议一个更好的解决问题的方法吗?我会编辑pos