asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – MVC 5 Owin Facebook Auth导致空参考异常大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在Visual studio 2013中的一个新的R_349_11845@VC 5项目中设置集成的OWIN Facebook身份验证。我已经按照本教程配置了应用和密钥:

http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

但是,我在AccountController中从此调用中抛出NullReferenceException:

[AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallBACk(String returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

我已经检查了Fiddler的回应,并且得到了Facebook的成功回应,但仍然收到这个错误。响应如下所示:

{"id":"xxx","name":"xxx","first_name":"xxx","last_name":"xxx","link":
"https:\/\/www.facebook.com\/profile.php?id=xxx","LOCATIOn":{"id":"xxx","name":"xxx"},"gender":"xxx","timezone":1,"locale":"en_GB","verified":true,"updated_time":"2013-10-23T10:42:23+0000"}

我在httphttps调试时得到这个。我猜这是一个框架错误,但是迄今为止已经通过反射器来绘制了一个空白的诊断。

解决方法

这可能是身份OWIN扩展代码中的错误。我无法重现这个问题,因为我的Facebook有效载荷总是返回一个用户名字段在json,这是从你的fb响应中丢失。我不太清楚为什么不在那里

身份owin扩展方法中的代码对于身份的名称声明没有空值检查,与用户名字段相同。我们在内部提交了一个错误。

为了解决这个问题,您可以尝试使用以下代码替换ExternalLoginCallBACk方法:

[AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallBACk(String returnUrl)
    {
        var result = await AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookiE);
        if (result == null || result.Identity == null)
        {
            return RedirectToAction("Login");
        }

        var idClaim = result.Identity.FindFirst(ClaimTypes.NamEIDentifier);
        if (idClaim == null)
        {
            return RedirectToAction("Login");
        }

        var login = new UserLoginInfo(idClaim.Issuer,idClaim.value);
        var name = result.Identity.Name == null ? "" : result.Identity.Name.replace(" ","");

        // Sign in the user with this external login provider if the user already has a login
        var user = await UseRMANager.FindAsync(login);
        if (user != null)
        {
            await SignInAsync(user,isPersistent: falsE);
            return RedirectToLocal(returnUrl);
        }
        else
        {
            // If the user does not have an account,then prompt the user to create an account
            ViewBag.ReturnUrl = returnUrl;
            ViewBag.LoginProvider = login.LoginProvider;
            return View("ExternalLoginConfirmation",new ExternalLoginConfirmationViewModel { UserName = name });
        }
    }

当没有用户名从Facebook /谷歌返回时,代码将默认用户名设置为空。

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc – MVC 5 Owin Facebook Auth导致空参考异常全部内容,希望文章能够帮你解决asp.net-mvc – MVC 5 Owin Facebook Auth导致空参考异常所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。