MVC 5 Owin Facebook Auth results in Null Reference Exception

后端 未结 11 1177
忘了有多久
忘了有多久 2020-12-01 11:52

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

11条回答
  •  无人及你
    2020-12-01 12:39

    I came across this post a few days ago but unfortunately none of the above solutions worked for me. so here is how I managed to fix it and get the email from Facebook.

    • Update following NuGet Pacakges
      • Microsoft.Owin to version 3.1.0-rc1
      • Microsoft.Owin.Security to version 3.1.0-rc1
      • Microsoft.Owin.Security.Cookies to version 3.1.0-rc1
      • Microsoft.Owin.Security.OAuth to version 3.1.0-rc1
      • Microsoft.Owin.Security.Facebook to version 3.1.0-rc1

    Then add the following code to the Identity Startup class

    var facebookOptions = new FacebookAuthenticationOptions()
            {
                AppId = "your app id",
                AppSecret = "your app secret",
                BackchannelHttpHandler = new FacebookBackChannelHandler(),
                UserInformationEndpoint = "https://graph.facebook.com/v2.8/me?fields=id,name,email,first_name,last_name",
                Scope = { "email" }
            };
    
            app.UseFacebookAuthentication(facebookOptions);
    

    This is the definition class for FacebookBackChannelHandler():

    using System;
    using System.Net.Http;
    
    public class FacebookBackChannelHandler : HttpClientHandler
    {
        protected override async System.Threading.Tasks.Task SendAsync(
            HttpRequestMessage request,
            System.Threading.CancellationToken cancellationToken)
        {
            // Replace the RequestUri so it's not malformed
            if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
            {
                request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token"));
            }
    
            return await base.SendAsync(request, cancellationToken);
        }
    }
    

提交回复
热议问题