2017-04-11 3 views
1

Ich habe Probleme beim Laden der Facebook-Identität. Die Seite wird richtig auf Facebook umgeleitet, aber wenn ich auf meine Seite zurückkomme, kann ich die Identität von Owin nicht lesen.Laden der Facebook-Identität in ASP.NET unter Verwendung von Owin

Hier ist meine 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); 

Und mein PageBase.cs zu behandeln Login:

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; 
     } 
    } 

In user.Identities hatte ich als wahr IsAuthenticated. Jetzt habe ich immer falsch, und ich habe keine Identität. Fordert für Facebook.

Wie kann ich Facebook-Benutzerinformationen über den angemeldeten Benutzer erhalten, resp. Wie kann ich Facebook Identität bekommen?

Antwort

0

Facebook hat kürzlich einige seiner OAuth-Endpunkte veraltet. Wenn Sie das NuGet-Paket Microsoft.Owin.Security.Facebook 3.0.1 verwenden, müssen Sie auf die neueste Version aktualisieren (die auch mehrere verwandte Pakete aktualisiert).

  • Direkt am Projekt klicken
  • Select "Manage NuGet Packages"
  • Select "Installiert"
  • Suche nach "Facebook" oder das Paket aus der Liste
  • Select „Microsoft.Owin finden. Security.Facebook“und klicken Sie auf "Update"

Referenz: https://github.com/aspnet/AspNetKatana/issues/38

Verwandte Themen