【问题标题】:NullReferenceException in DotNetOpenAuthDotNetOpenAuth 中的 NullReferenceException
【发布时间】:2013-12-01 03:49:41
【问题描述】:

我在我的 ASP.NET MVC 应用程序中找到了导致 NullReferenceException 的缺陷。是我破坏了这个,还是应该破坏大多数安装的 DotNetOpenAuth?

我明白了:

[NullReferenceException: Object reference not set to an instance of an object.]
   DotNetOpenAuth.AspNet.OpenAuthSecurityManager.GetUsername(HttpContextBase context) +27
   DotNetOpenAuth.AspNet.OpenAuthSecurityManager.RequestAuthentication(String returnUrl) +341
   Controllers.LoginController.Index() +226

这是Index() 方法。请注意,它目前在生产中返回 User,在开发中返回 OK。这些显然是暂时的。注释代码来自我在下面引用的最后一个 URL,在那里我减少了我和问题之间的代码量。不过,堆栈跟踪仍然相似。

public virtual ActionResult Index()
{
    if (HttpContext == null) return Content("HttpContext");
    if (HttpContext.User == null) return Content("User");
    if (HttpContext.User.Identity == null) return Content("Identity");
    return Content("OK");
    new OpenAuthSecurityManager(HttpContext, s_fbClient, OAuthDataProvider.Instance)
        .RequestAuthentication(Url.Action(Actions.Callback()));
    // OAuthWebSecurity
    //  .RequestAuthentication("facebook", Url.Action(Actions.Callback()));
    return null;
}

出现异常是因为HttpContext.User 为空。 Here's the DotNetOpenAuth.AspNet library source 表示失败的方法。

private static string GetUsername(HttpContextBase context) {
    string username = null;
        if (context.User.Identity.IsAuthenticated) {
            username = context.User.Identity.Name;
        }
    return username ?? string.Empty;
}

User 显然有been nullable ever since IIS Integrated Mode was available。这解释了为什么我在开发中看不到它,因为我正在使用默认值运行 IIS Express,但它没有解释为什么我找不到有关该缺陷的任何信息。集成模式于 2007 年发布,DotNetOpenAuth 仍在维护。 Microsoft 文档对此设置进行了说明:

经典模式:仅当应用程序在 应用程序池无法在集成模式下运行。

我一定遗漏了什么,因为似乎每个人都应该有这个问题。

我是否以某种方式 NuGet'ed 一个非维护库?这似乎很奇怪,因为它显示它是一周前刚刚更新的。但是,当我按照 NuGet 的文档链接进行操作时,the source code I arrive at 似乎甚至没有 AspNet 命名空间,这是我的异常出现的地方。

编辑:我目前使用的唯一相关包是 DotNetOpenAuth.AspNet(它有 8 个依赖项),最后一次发布不到一周前。我也尝试过其他软件包。我不需要SimpleAuth 或任何WebMatrix 爵士乐。在解决这个问题的过程中,我尝试按照http://techblog.dorogin.com/2013/06/Microsoft.AspNet.WebPages.OAuth.html 的描述切换库

编辑:记录了与此相关的缺陷,但似乎该库可能无法维护。 https://github.com/DotNetOpenAuth/DotNetOpenAuth/issues/317#issuecomment-29580565

编辑:等待堆栈跟踪,它可能与 MVC 5 Owin Facebook Auth results in Null Reference Exception 相同的缺陷

【问题讨论】:

  • 在我看来,您注入的 context 是错误的。
  • 如果我理解正确,迈克尔,我不会注入 context。您正在查看的是来自 DotNetOpenAuth 库的一段代码,其完整源代码已在上面链接。
  • 我认为他是在暗示你传入了错误的上下文。你能显示来自 LoginController 的 Index 方法的代码吗?
  • 当然,菲尔。贴在上面。请注意,对于 WebMatrix 包,我什至没有传递 HttpContext,因为库从静态属性中提取了它。
  • 我不能代表 Michael,但我误读了堆栈跟踪,并认为您是直接调用 GetUserName。

标签: asp.net-mvc oauth nuget dotnetopenauth integrated-pipeline-mode


【解决方案1】:

这确实是 DotNetOpenAuth.AspNet 代码中的一个缺陷。不幸的是,不再维护 DotNetOpenAuth 库。修改源代码:

private static string GetUsername(HttpContextBase context) {
    string username = null;
        if (context.User != null && context.User.Identity.IsAuthenticated) {
            username = context.User.Identity.Name;
        }
    return username ?? string.Empty;
}

当然可以。

【讨论】:

    猜你喜欢
    • 2010-11-05
    • 2011-07-24
    • 2013-03-12
    • 2016-09-20
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多