5

Tôi đang tìm hiểu cách làm việc với OWIN bên ngoài bằng cách sử dụng mẫu MVC mặc định trong VS 2015. Tôi đã bật Facebook auth và thêm phạm vi "email", dự kiến ​​email của người dùng sẽ được trả lại bởi Facebook sau khi người dùng được xác thực (theo số this). Tuy nhiên, không có email trong ngữ cảnh.Đối tượng JSON của người dùng và ngữ cảnh.Email cũng là rỗng.Đang truy xuất email Facebook của người dùng trên đăng nhập OWIN

Đây là mã có liên quan trong Startup.Auth.cs

var facebookOptions = new FacebookAuthenticationOptions 
     { 
      AppId = "XXXXXX", 
      AppSecret = "XXXXXX", 
      Provider = new FacebookAuthenticationProvider 
      { 
       OnAuthenticated = context => 
       { 
        // Retrieve the OAuth access token to store for subsequent API calls 
        var accessToken = context.AccessToken; 

        // Retrieve the username 
        var facebookUserName = context.UserName; 

        // WHY IS IT EMPTY? 
        var facebookEmail = context.Email; 

        // You can even retrieve the full JSON-serialized user 
        var serializedUser = context.User; 

        return Task.FromResult(0); 
       } 
      } 
     }; 

     facebookOptions.Scope.Add("email"); 

     app.UseFacebookAuthentication(facebookOptions); 

Bất cứ ý tưởng gì là mất tích? Tại sao địa chỉ email facebook không được trả lại?

+4

thông tin có khả năng hữu ích ở đây: http: //stackoverflow.com/a/32636149/3130094 –

+1

Cảm ơn @JamieDunstan - đó là hữu ích! – Andrey

Trả lời

4

Hóa ra đã có sự thay đổi đột phá trong Facebook API phiên bản 2.4 nơi bạn phải chỉ định các trường bạn muốn truy xuất. Yêu cầu đồ thị từng là:

https://graph.facebook.com/v2.3/me?access_token=XXXXX 

nhưng vì lý do hiệu suất như của FB API v2.4 bạn cũng phải xác định fileds bạn muốn lấy trong phạm vi:

https://graph.facebook.com/v2.4/me?fields=id,name,email&access_token=XXXXX 

Microsoft FB thực hiện khách hàng theo mặc định gắn access_token vào chuỗi truy vấn như "access_token?" dẫn đến yêu cầu bị hỏng (thêm dấu hỏi):

https://graph.facebook.com/v2.4/me?fields=id,name,email?access_token=XXXXX

Vì vậy, để khắc phục rằng chúng ta cần phải sử dụng một BackchannelHttpHandler tùy chỉnh. Đầu tiên, chúng ta tạo ra lớp endpoint:

public class FacebookBackChannelHandler : HttpClientHandler 
    { 
     protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) 
     { 
      if (!request.RequestUri.AbsolutePath.Contains("/oauth")) 
      { 
       request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token")); 
      } 

      return await base.SendAsync(request, cancellationToken); 
     } 
    } 

Và sau đó chúng tôi cung cấp nó trong các tùy chọn auth facebook cùng với xác định một cách rõ ràng UserInformationEndpoint:

var facebookAuthOptions = new FacebookAuthenticationOptions 
     { 
      AppId = ConfigurationManager.AppSettings["FacebookAppId"], 
      AppSecret = ConfigurationManager.AppSettings["FacebookAppSecret"], 
      BackchannelHttpHandler = new FacebookBackChannelHandler(), 
      UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email", 
      Scope = { "email" } 
      <.....> 
     }; 

Từ: https://stackoverflow.com/a/32636149/3130094

Các vấn đề liên quan