0

我在我的ASP.NET Core應用程序中實現了LinkedIn身份驗證,但出於某種原因,我沒有從LinkedIn獲取任何數據。我在哪裏犯錯誤?ASP.NET Core LinkedIn身份驗證沒有獲取數據

在Startup.cs的ConfigureServices,我有以下幾點:

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

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

在Startup.cs的配置,我有以下代碼:

app.AuthenticationForMyApp(
       Configuration["linkedInClientId"], 
       Configuration["linkedInClientSecret"], 
       Configuration["linkedInCallback"]); 

我的代碼看起來像這樣在AuthenticationForMyApp類:

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永遠不會預填充,因爲發送「userinfo」請求以從遠程OAuth2提供程序檢索用戶配置文件取決於您。

更新OnCreatingTicketLinkedInCallback發送一個HTTP請求到LinkedIn的用戶信息終端和檢索所需或切換到aspnet-contrib LinkedIn middleware會爲你做的,通過@Tratcher的建議JSON響應。