【问题标题】:WebApp-WebAPI-OpenIDConnect-DotNet not deploying on azureWebApp-WebAPI-OpenIDConnect-DotNet 未部署在 azure 上
【发布时间】:2015-04-18 09:49:51
【问题描述】:

我已经创建了 WebApp-WebAPI-OpenIDConnect-DotNet 的示例项目。参考项目可在git 获得。

我按照 git repo 中描述的所有步骤进行操作。我的示例项目在本地主机上正常运行。但是当我在 Azure 上部署我的项目时,它会出现以下错误。

请检查下面的堆栈跟踪。

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   CMS.Web.Utils.NaiveSessionCache.Load() in c:\a\src\CMS.Web\CMS.Web\Utils\NaiveSessionCache.cs:30
   CMS.Web.Utils.NaiveSessionCache..ctor(String userId) in c:\a\src\CMS.Web\CMS.Web\Utils\NaiveSessionCache.cs:23
   CMS.Web.Startup.<ConfigureAuth>b__2(AuthorizationCodeReceivedNotification context) in c:\a\src\CMS.Web\CMS.Web\App_Start\Startup.Auth.cs:83
   Microsoft.Owin.Security.OpenIdConnect.<AuthenticateCoreAsync>d__1a.MoveNext() +4995
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +22
   Microsoft.Owin.Security.OpenIdConnect.<AuthenticateCoreAsync>d__1a.MoveNext() +6529
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Security.Infrastructure.<BaseInitializeAsync>d__0.MoveNext() +595
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +264
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +191
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +665
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +191
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<DoFinalWork>d__2.MoveNext() +189
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +69
   Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +64
   System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

    标签: asp.net-mvc azure openid


    【解决方案1】:

    我的案子是一样的。我的项目正在开发环境中工作,并在将其发布到生产环境后在同一行上获取 NullRefenceException。经过处理,我发现 Request.Current 没问题,但 Request.Current.Session 为空。所以,我把我的代码改成了这样:

    public void Load()
        {
            lock (FileLock)
            {
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                {
    
                    if (HttpContext.Current.Session[CacheId] == null)
                        throw new System.NullReferenceException("CacheId is null");
    
                    Deserialize((byte[]) HttpContext.Current.Session[CacheId]);
                }
            }
        }
    

    之后它给了我 WebExcetpion 400 (BAD REQUEST),这发生在会话创建之前。

    为了处理这种错误,我更改了 Startup.Auth.cs:

                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        AuthenticationFailed = OnAuthenticationFailed,
                        ...
                    }
    

    然后:

       private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
        {
            UtilLogger.PublishException(notification.Exception);
    
            notification.HandleResponse();
            notification.Response.Redirect($"{postLogoutRedirectUri}?message={notification.Exception.Message}");
            return Task.FromResult(0);
        }
    

    【讨论】:

      【解决方案2】:

      我看到一个类似的问题,HttpContext.Current 在获取令牌时最初为空,当它尝试将该令牌保存到缓存时,您会在 Load/Persist 方法中看到此错误。不确定这是否是预期的,但在 Load 和 Persist 方法中添加对 HttpContext.Current 的非空检查将缓解对象空引用异常。

      【讨论】:

        猜你喜欢
        • 2019-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-09
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        相关资源
        最近更新 更多