0

ASP.NET CoreアプリケーションでLinkedIn認証を実装していますが、何らかの理由でLinkedInからデータが取得されません。私はどこでミスをしていますか?ASP.NET Core LinkedIn認証データ取得中

Startup.csのConfigureServicesで

、私は次のようしている:Startup.csの設定で

  services.AddMvc(options => 
      { 
       options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build())); 
      }); 

      services.AddAuthentication(options => 
      { 
       options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; 
      }); 

、I次のコードを持っている:

私のコードはAuthenticationForMyAppで次のようになります
app.AuthenticationForMyApp(
       Configuration["linkedInClientId"], 
       Configuration["linkedInClientSecret"], 
       Configuration["linkedInCallback"]); 

クラス:

public static void AuthenticationForMyApp(this IApplicationBuilder app, string linkedInClientId, string linkedInSecret, string linkedInCallback) 
     { 
      app.UseCookieAuthentication(new CookieAuthenticationOptions() 
      { 
       AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme, 
       LoginPath = new PathString("/login"), 
       AccessDeniedPath = new PathString("/unauthorized"), 
       AutomaticAuthenticate = true, 
       AutomaticChallenge = true 
      }); 

      app.UseOAuthAuthentication(new OAuthOptions() 
      { 
       AuthenticationScheme = "LinkedIn", 
       ClientId = linkedInClientId, 
       ClientSecret = linkedInSecret, 
       CallbackPath = linkedInCallback, 
       AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization", 
       TokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken", 
       UserInformationEndpoint = "https://api.linkedin.com/v1/people/~:(id,formatted-name,email-address,picture-url)", 
       Scope = { "r_basicprofile", "r_emailaddress" }, 
       Events = new OAuthEvents() 
       { 
        OnCreatingTicket = OnCreatingTicketLinkedInCallback, 
        OnTicketReceived = OnTicketReceivedCallback 
       } 
      }); 

     } 
     private static Task OnCreatingTicketLinkedInCallback(OAuthCreatingTicketContext ctx) 
     { 
      var token = ctx.AccessToken; 

      // *** Here ctx.User has no data about the user!!! 
      var firstName = ctx.User["first_name"]; 
      var lastName = ctx.User["last_name"]; 
      var gender = ctx.User["gender"]; 
      var email = ctx.User["email"]; 
      var identity = ctx.Identity as ClaimsIdentity; 

      if (firstName != null) 
      { 
       identity.AddClaim(new Claim(ClaimTypes.GivenName, firstName.ToString(), ClaimValueTypes.String, "LinkedIn")); 
      } 
      if (lastName != null) 
      { 
       identity.AddClaim(new Claim(ClaimTypes.Surname, lastName.ToString(), ClaimValueTypes.String, "LinkedIn")); 
      } 
      if (email != null) 
      { 
       identity.AddClaim(new Claim(System.Security.Claims.ClaimTypes.Email, email.ToString(), ClaimValueTypes.String, "LinkedIn")); 
      } 
      if (gender != null) 
      { 
       identity.AddClaim(new Claim(ClaimTypes.Gender, gender.ToString(), ClaimValueTypes.String, "LinkedIn")); 
      } 

      identity.AddClaim(new Claim("linkedin.token", token)); 

      return Task.FromResult(true); 
     } 
     private static Task OnTicketReceivedCallback(TicketReceivedContext ctx) 
     { 
      var identity = ctx.Principal.Identity as ClaimsIdentity; 
      if (identity == null) 
      { 
       ctx.ReturnUri = "/unauthorized"; 
      } 
      else 
      { 
       if (ctx.Properties.Items.ContainsKey("returnUrl")) 
       { 
        ctx.ReturnUri += "?returnUrl=" + ctx.Properties.Items["returnUrl"]; 
       } 
      } 
      return Task.FromResult(true); 
     } 
+0

すでにLinkedInのパッケージがあります:https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/blob/dev/src/AspNet.Security.OAuth.LinkedIn/LinkedInAuthenticationExtensions.cs – Tratcher

+0

ありがとうあなたの提案については、私は不要なコード/パッケージ/ライブラリで私のアプリを膨らませたくない。私は本当にこのコードを動作させることができるはずです。 – Sam

答えて

0

OAuth2汎用ミドルウェアをそのまま使用する場合は、OAuthCreatingTicketContext.Userは事前に設定されません。これは、リモートOAuth2プロバイダからユーザープロファイルを取得するための「userinfo」リクエストを送信する必要があるためです。

更新OnCreatingTicketLinkedInCallbackはLinkedInののUserInfoエンドポイントにHTTPリクエストを送信し、あなたが必要とするか@Tratcherにより示唆されるように、あなたのためにそれを行うだろうaspnet-contrib LinkedIn middlewareに切り替えJSONレスポンスを取得します。

関連する問題