3

我在 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

4 回答 4

5

对于访问此页面的其他人,我遇到了同样的问题,但解决方案要简单得多。正如上面评论中提到的,接受的答案很糟糕。还提到的是 SignalR 默认使用MachineKeyDataProtectorfor IProtectedDataMapHubs并且MapConnection都调用一个向依赖解析器InitializeProtectedData注册的函数。MachineKeyDataProtector

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

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

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

GlobalHost.DependencyResolver = 
                     new StructureMapDependencyResolver(ObjectFactory.Container);
RouteTable.Routes.MapConnection<SomeEndpoint>("SomeEndpoint", "SomeEndpointUrl");
于 2013-09-13T00:23:53.380 回答
2

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

请注意,使用 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>();

    ...
于 2013-04-03T13:44:26.243 回答
0

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

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

于 2013-09-03T19:08:48.127 回答
-1

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

于 2013-03-13T19:06:55.653 回答