2017-03-08 2 views
0

私は外部のログインを(Facebookへ)有効にする私のアプリケーションでASP.NETコアアイデンティティを使用しています。私の質問は、Facebookのログインページにリダイレクトされ、OKをクリックした後、FacebookによってExternalLoginCallbackメソッドにリダイレクトされた後、FacebookMiddlewareがFacebookのボタンを[ログイン]ページでクリックした後のことですFacebookMiddlewareのすべてのスキームとそれは何ですか?外部ログインでFacebookMiddlewareの役割は何ですか?

答えて

0

名前が言うように、FacebookMiddlewareはFacebookを使用しているユーザーを認証しているコンポーネントです。

UseFacebookAuthentication呼び出しを呼び出すと、基本的にFacebookMiddlewareが接続されます。

FacebookMiddlewareが内部で何がそう、ここで誰かが同じ問題に遭遇し念のFacebookHandler

internal class FacebookHandler : OAuthHandler<FacebookOptions> 
     { 
      public FacebookHandler(HttpClient httpClient) : base(httpClient) 
      { 
      } 

      protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens) 
      { 
       string text = QueryHelpers.AddQueryString(this.get_Options().get_UserInformationEndpoint(), "access_token", tokens.get_AccessToken()); 
       if (this.get_Options().SendAppSecretProof) 
       { 
        text = QueryHelpers.AddQueryString(text, "appsecret_proof", this.GenerateAppSecretProof(tokens.get_AccessToken())); 
       } 
       if (this.get_Options().Fields.get_Count() > 0) 
       { 
        text = QueryHelpers.AddQueryString(text, "fields", string.Join(",", this.get_Options().Fields)); 
       } 
       HttpResponseMessage httpResponseMessage = await this.get_Backchannel().GetAsync(text, this.get_Context().get_RequestAborted()); 
       if (!httpResponseMessage.get_IsSuccessStatusCode()) 
       { 
        throw new HttpRequestException(string.Format("Failed to retrieve Facebook user information ({0}) Please check if the authentication information is correct and the corresponding Facebook Graph API is enabled.", httpResponseMessage.get_StatusCode())); 
       } 
       JObject jObject = JObject.Parse(await httpResponseMessage.get_Content().ReadAsStringAsync()); 
       AuthenticationTicket authenticationTicket = new AuthenticationTicket(new ClaimsPrincipal(identity), properties, this.get_Options().get_AuthenticationScheme()); 
       OAuthCreatingTicketContext oAuthCreatingTicketContext = new OAuthCreatingTicketContext(authenticationTicket, this.get_Context(), this.get_Options(), this.get_Backchannel(), tokens, jObject); 
       string id = FacebookHelper.GetId(jObject); 
       if (!string.IsNullOrEmpty(id)) 
       { 
        identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", id, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
       } 
       string ageRangeMin = FacebookHelper.GetAgeRangeMin(jObject); 
       if (!string.IsNullOrEmpty(ageRangeMin)) 
       { 
        identity.AddClaim(new Claim("urn:facebook:age_range_min", ageRangeMin, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
       } 
       string ageRangeMax = FacebookHelper.GetAgeRangeMax(jObject); 
       if (!string.IsNullOrEmpty(ageRangeMax)) 
       { 
        identity.AddClaim(new Claim("urn:facebook:age_range_max", ageRangeMax, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
       } 
       string birthday = FacebookHelper.GetBirthday(jObject); 
       if (!string.IsNullOrEmpty(birthday)) 
       { 
        identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth", birthday, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
       } 
       string email = FacebookHelper.GetEmail(jObject); 
      if (!string.IsNullOrEmpty(email)) 
      { 
       identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", email, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string firstName = FacebookHelper.GetFirstName(jObject); 
      if (!string.IsNullOrEmpty(firstName)) 
      { 
       identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname", firstName, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string gender = FacebookHelper.GetGender(jObject); 
      if (!string.IsNullOrEmpty(gender)) 
      { 
       identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender", gender, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string lastName = FacebookHelper.GetLastName(jObject); 
      if (!string.IsNullOrEmpty(lastName)) 
      { 
       identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", lastName, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string link = FacebookHelper.GetLink(jObject); 
      if (!string.IsNullOrEmpty(link)) 
      { 
       identity.AddClaim(new Claim("urn:facebook:link", link, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string location = FacebookHelper.GetLocation(jObject); 
      if (!string.IsNullOrEmpty(location)) 
      { 
       identity.AddClaim(new Claim("urn:facebook:location", location, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string locale = FacebookHelper.GetLocale(jObject); 
      if (!string.IsNullOrEmpty(locale)) 
      { 
       identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/locality", locale, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string middleName = FacebookHelper.GetMiddleName(jObject); 
      if (!string.IsNullOrEmpty(middleName)) 
      { 
       identity.AddClaim(new Claim("urn:facebook:middle_name", middleName, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string name = FacebookHelper.GetName(jObject); 
      if (!string.IsNullOrEmpty(name)) 
      { 
       identity.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", name, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      string timeZone = FacebookHelper.GetTimeZone(jObject); 
      if (!string.IsNullOrEmpty(timeZone)) 
      { 
       identity.AddClaim(new Claim("urn:facebook:timezone", timeZone, "http://www.w3.org/2001/XMLSchema#string", this.get_Options().get_ClaimsIssuer())); 
      } 
      await this.get_Options().get_Events().CreatingTicket(oAuthCreatingTicketContext); 
      return oAuthCreatingTicketContext.get_Ticket(); 
     } 

     private string GenerateAppSecretProof(string accessToken) 
     { 
      string result; 
      using (HMACSHA256 hMACSHA = new HMACSHA256(Encoding.get_ASCII().GetBytes(base.get_Options().AppSecret))) 
      { 
       byte[] array = hMACSHA.ComputeHash(Encoding.get_ASCII().GetBytes(accessToken)); 
       StringBuilder stringBuilder = new StringBuilder(); 
       for (int i = 0; i < array.Length; i++) 
       { 
        stringBuilder.Append(array[i].ToString("x2", CultureInfo.get_InvariantCulture())); 
       } 
       result = stringBuilder.ToString(); 
      } 
      return result; 
     } 

     protected override string FormatScope() 
     { 
      return string.Join(",", base.get_Options().get_Scope()); 
     } 
    } 
} 
+0

あなたのお返事ありがとうございました これは私が探している答えではありません... – Issac

0

のコードですが、コード自体よりも優れについて説明していない

FacebookHandler

を使用しています。エラーは、この作品からスローされますが、ミドルウェアからのコード:要するに

var response = Backchannel.GetAsync(endpoint).Result; if (!response.IsSuccessStatusCode) { throw new HttpRequestException($"Failed to retrieve Facebook user information ({response.StatusCode}) Please check if the authentication information is correct and the corresponding Facebook Graph API is enabled."); }

、開発エラーが発生した場合は、一般的なエラーメッセージをスローします。実際のエラーが分かっていることを知るためには、Fiddlerなどのツールを使ってFacebookからの応答をキャプチャできます。私の場合、以下のようになります。

{"error":{"message": "構文エラー\ "文字\ 5:email; first_name; last_name"、 "type": "OAuthException"、 "code":2500、 "fbtrace_id": "xxxxxxxx"}}の代わりに、

関連する問題