MVC 5 Owin Facebook Auth 导致空引用异常 [英] MVC 5 Owin Facebook Auth results in Null Reference Exception

查看:35
本文介绍了MVC 5 Owin Facebook Auth 导致空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Visual Studio 2013 的新 MVC 5 项目中设置集成的 OWIN Facebook 身份验证.我已经按照本教程配置了应用程序和密钥:

I'm trying to setup integrated OWIN Facebook authentication in a new MVC 5 project in Visual Studio 2013. I have configured apps and keys as per this tutorial:

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

但是,我在 AccountController 中的这个调用中抛出了 NullReferenceException:

However, I'm getting a NullReferenceException thrown from this call in the AccountController:

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

我已经检查了 Fiddler 中的响应,并且从 Facebook 收到了看​​似成功的响应,但仍然收到此错误.响应如下:

I already checked the response in Fiddler and am getting what appears to be a success response from Facebook, but still get this error. The response looks like this:

{"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"}

我在 http 和 https 中调试时得到这个.我猜这是一个框架错误,但到目前为止,通过反射器对此进行了诊断.

I get this when debugging in http as well as https. I'm guessing this is a framework bug but have so far drawn a blank diagnosing this through reflector.

推荐答案

这可能是身份 OWIN 扩展代码中的错误.我无法重现该问题,因为我的 Facebook 负载总是在 json 中返回一个用户名字段,而您的 fb 响应中缺少该字段.我不太确定为什么它不在那里.

This probably is a bug in identity OWIN extension code. I can't repro the issue as my facebook payload always returns a username field in json, which is missing from your fb response. I am not quite sure why it's not there.

身份 owin 扩展方法中的代码没有对与用户名字段相同的身份名称声明进行空检查.我们已经在内部为它提交了一个错误.

The code in identity owin extension method doesn't have a null check for the identity's name claim which is same as the username field. We have filed a bug for it internally.

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

In order to workaround this issue, could you try replacing your ExternalLoginCallback method with following code:

   [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/google 没有用户名时,代码会将默认用户名设置为空.

The code will set default user name as empty when there is no username back from facebook/google.

这篇关于MVC 5 Owin Facebook Auth 导致空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆