AzureWebsites上的SignalR CryptographicException

时间:2013-03-13 18:36:29

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

我使用部署在Azure网站中的SignalR获得了此异常。它在调试环境中工作正常。它是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
你知道吗? 感谢

4 个答案:

答案 0 :(得分:5)

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

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

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

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

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

答案 1 :(得分:2)

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

请注意,使用Visual Studio开发服务器时未遇到此问题,但转移到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;
        }
    }
}

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

    ...

答案 2 :(得分:0)

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

将使用

MachineKeyProtectedData代替默认实现。

答案 3 :(得分:-1)

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