NullReferenceException in DotNetOpenAuth



我在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为null,因此出现异常。以下是该失败方法的DotNetOpenAuth.AspNet库源。

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

自从IIS集成模式可用以来,User显然一直可以为null。这解释了为什么我在开发中没有看到它,因为我使用默认值运行IIS Express,但这并不能解释为什么我找不到任何关于缺陷的信息。集成模式于2007年发布,DotNetOpenAuth仍然保留。微软文档这样说关于设置:

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

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

我是不是不知怎么弄丢了一个没有维护的图书馆?这似乎很奇怪,因为它显示它一周前刚刚更新。但是,当我使用NuGet中的文档链接时,我得到的源代码似乎甚至没有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

EDIT:等待堆栈跟踪,它可能与MVC 5 Owin Facebook Auth导致空引用异常的缺陷相同

这确实是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;
}

当然可以。

最新更新