2017-04-11 13 views
1

我在加載Facebook標識時遇到問題。該頁面正確地重定向到Facebook,但當我回到我的網站時,我無法從Owin讀取身份。無法使用Owin在ASP.NET中加載Facebook標識

這裏是我的Startup.Auth.cs

var cookieOptions = new CookieAuthenticationOptions 
{ 
    LoginPath = new PathString("/default.aspx") 
}; 

app.UseCookieAuthentication(cookieOptions); 
app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType); 
var facebookAuthenticationOptions = new FacebookAuthenticationOptions() 
{ 
    AppId = ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First() != null ? ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First().AppId : string.Empty, 
    AppSecret = ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First() != null ? ExternalConnections.Where(o => o.Name == "Facebook" && o.Exist && o.SiteId == site.Key).First().AppSecretKey : string.Empty, 
    SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType(), 
    CallbackPath = new PathString("/Default.aspx/") 
}; 

facebookAuthenticationOptions.Provider = new FacebookAuthenticationProvider 
{ 
    OnAuthenticated = async context => 
    { 
     context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken)); 
     foreach (var claim in context.User) 
     { 
      var claimType = string.Format("urn:facebook:{0}", claim.Key); 
      string claimValue = claim.Value.ToString(); 
      if (!context.Identity.HasClaim(claimType, claimValue)) 
       context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook")); 
     } 
    } 
}; 

facebookAuthenticationOptions.Scope.Add("email"); 
facebookAuthenticationOptions.Scope.Add("public_profile"); 

app2.UseFacebookAuthentication(facebookAuthenticationOptions); 

而且我PageBase.cs處理登錄:

var ctx = Context.GetOwinContext(); 
var user = ctx != null ? ctx.Authentication.User : new ClaimsPrincipal(); 
if (user != null && user.Identities.Count() > 0 && user.Identity.IsAuthenticated) 
{ 
    var claimsPrincipal = new ClaimsPrincipal(user.Identity); 
    Thread.CurrentPrincipal = claimsPrincipal; 
    var identity = (ClaimsPrincipal)Thread.CurrentPrincipal; 

    if (identity.Claims.Count() > 0) 
    { 
     switch (identity.Claims.First().OriginalIssuer) 
     { 
      case "Facebook": 

       if (!featureExternalLoginFacebook) { break; } 

       id = identity.Claims.Where(c => c.Type == "urn:facebook:id").Select(c => c.Value).SingleOrDefault(); 
       name = identity.Claims.Where(c => c.Type == ClaimTypes.Name).Select(c => c.Value).SingleOrDefault(); 
       accessToken = identity.Claims.Where(c => c.Type == "FacebookAccessToken").Select(c => c.Value).SingleOrDefault(); 

       var url = string.Format("https://graph.facebook.com/v2.5/{0}?fields=email,first_name,last_name&access_token={1}", id, accessToken); 

       var req = WebRequest.Create(url); 
       req.Method = "GET"; 
       req.ContentType = "application/json"; 

       using (var res = new StreamReader(req.GetResponse().GetResponseStream())) 
       { 
        dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(res.ReadToEnd()); 

        if (result.id != null && result.id == id) 
        { 
         email = result.email != null ? result.email : string.Empty; 
         firstName = result.first_name != null ? result.first_name : string.Empty; 
         lastName = result.last_name != null ? result.last_name : string.Empty; 
        } 
       } 

       break; 
     } 

     externalApp = identity.Claims.First().OriginalIssuer; 

     if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(email)) 
     { 
      return true; 
     } 
    } 

在user.Identities,我已經IsAuthenticated爲真。現在我一直都是假的,而且我沒有身份。對Facebook的讚譽。

我如何獲得有關登錄用戶的Facebook用戶信息,我如何獲得Facebook身份?

回答

0

Facebook最近棄用了一些OAuth端點。如果您使用NuGet軟件包Microsoft.Owin.Security.Facebook 3.0.1,則需要更新到最新版本(它也將更新幾個相關軟件包)。

  • 右鍵單擊項目
  • 選擇 「管理的NuGet包」
  • 選擇 「安裝」
  • 搜索 「臉譜」 或從列表
  • 選擇「Microsoft.Owin找到包。 Security.Facebook」,然後點擊 「更新」

參考:https://github.com/aspnet/AspNetKatana/issues/38

相關問題