2015-04-26 13 views
13

Tôi đang sử dụng mã sau cho ExternalLoginCallback
Trong google mọi thứ đều OK. nhưng trong FacebookMicrosoftloginInfo.Email luôn là giá trị rỗng. Có gì sai với mã sau?ExternalLoginInfo Email luôn là null trong Microsoft và Facebook oauth2, MVC C#?

[AllowAnonymous] 
public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 
{ 
    ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); 
    if (loginInfo == null) 
    { 
     return RedirectToAction("Login"); 
    } 

    // loginInfo.Email is always null, so FindByEmailAsync throws an exception 
    UserIdentityModel user = await UserManager.FindByEmailAsync(loginInfo.Email); 

    if (user != null) 
    { 
     await SignInAsync(user, false); 
     return RedirectToLocal(returnUrl); 
    } 

    // If the user does not have an account, then prompt the user to create an account 
    ViewBag.ReturnUrl = returnUrl; 
    ViewBag.LoginProvider = loginInfo.Login.LoginProvider; 
    return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel 
    { 
     UserName = loginInfo.DefaultUserName, 
     Email = loginInfo.Email 
    }); 
} 

Tôi đang sử dụng các gói sau:

<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Security.Facebook" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Security.Google" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Security.MicrosoftAccount" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45" /> 
    <package id="Microsoft.Owin.Security.Twitter" version="3.0.1" targetFramework="net45" /> 
+0

Chưa sử dụng Microsoft oAuth, nhưng với Facebook, bạn cần phải yêu cầu cụ thể về quyền truy cập email trong cấu hình ứng dụng facebook của bạn, nếu không bạn sẽ không nhận được địa chỉ email. – Stephen

+0

Tôi đã thực hiện và tôi đã kiểm tra email. –

Trả lời

22

Tôi tìm thấy giải pháp, Chúng ta phải sử dụng FacebookMicrosoft như sau trong Startup.Auth.cs file:

// Facebook 
app.UseFacebookAuthentication(new FacebookAuthenticationOptions 
{ 
    AppId = "---", 
    AppSecret = "---", 
    Scope = { "email" } 
}); 

// Microsoft 
app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions() 
{ 
    ClientId = "---", 
    ClientSecret = "---", 
    Scope = { "wl.basic", "wl.emails" } 
}); 
+0

Tôi đang gặp phải sự cố tương tự để xác thực Yahoo OAuth2. Email là null sau khi xác thực thành công. Bạn có giải pháp xác thực Yahoo không? –

+0

Tôi có cùng một vấn đề: http://stackoverflow.com/questions/29998559/value-cannot-be-null-parameter-name-email-at-externallogincallback –

+8

Sau khi thực hiện việc này, vẫn 'info.Email' là rỗng cho tôi. –

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

Tạo một lớp học:

public class FacebookBackChannelHandler : HttpClientHandler 
    { 
     protected override async System.Threading.Tasks.Task<HttpResponseMessage> 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); 
     } 
    } 

phù hợp với tôi.

+0

Yeap đã hoạt động. –

+0

cũng hoạt động với tôi - thanx – yarg

0

Tôi đã thử câu trả lời của Mohammed. Không có con xúc xắc. Tôi đã thử câu trả lời của Bimal. Không có con xúc xắc.

Cuối cùng, tôi đã nâng cấp tất cả các tham chiếu OWIN của mình bằng Nuget. Bây giờ nó hoạt động.

Nếu các câu trả lời khác không hoạt động, hãy thử nâng cấp tất cả các gói Nuget của bạn lên tất cả các mục OWIN.

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