Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

c# - MVC5 Null Reference with facebook login

Im getting an null reference exception sometimes when I login with facebook using the out of the box ASP.NET mvc5 accounts controller.


Here is the dieing method :

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
        // Crashes on this line
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }
}

Im not sure how to debug this. A breakpoint and steeping though the code is no help... I end up looking at my Error.cshtml page. The error at that point is a simple object reference null exception and the inner exception is also null.


Edit

I updated to the latest Owins via Nuget, no change.

Edit 2

Took a look in fiddler, Facebook is returning a 200 with what looks like a correct profile as json.

Edit 3

So strange. Im testing with 3 facebook accounts. Two accounts are working fine, 1 does not. The failing one is returning with 200. I have removed the app references in facebook.. I get a app confirmation window, I click ok, and it dies.... so strange.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A quick solution for now.

You have to clear the Session before ExternalLoginCallback. Example.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    ControllerContext.HttpContext.Session.RemoveAll();

    // Request a redirect to the external login provider
    return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...