2013-11-21 190 views
8

我正在使用一個ASP.NET MVC 5網絡應用程序。我需要提供用戶登錄他們的LinkedIn帳戶。 MVC 5爲Facebook和Google提供登錄支持。但是我對LinkedIn如何實現這一點並不清楚。LinkedIn登錄ASP.NET MVC 5應用程序

在MVC 5中,LinkedIn沒有任何Katana支持。我應該採取什麼方法在MVC 5中實現這種特定行爲?任何建議將不勝感激。謝謝。

回答

0

終於找到了一種成功地做到這一點..!我複製了用於在Owin/Katana中執行Facebook身份驗證的代碼。你可以在這裏找到它。

Katana Project Source Code

我複製所有文件,並在下面的圖片中紅色方框內指定的文件夾。

enter image description here

在我自己的MVC 5 Web應用程序項目粘貼他們,我需要實現LinkedIn認證。將所有類更名爲LinkedIn而不是Facebook。

enter image description here

然後,我改變了代碼,使其適應LinkedIn。

  1. 常量類 將DefaultAuthenticationType更改爲LinkedIn而不是Facebook。
  2. LinkedInAuthenticationHandler類

    private const string TokenEndpoint = "https://www.linkedin.com/uas/oauth2/accessToken"; 
    private const string GraphApiEndpoint = "https://api.linkedin.com/v1/people/~"; 
    

    改變此密碼ApplyResponseChallengeAsync方法內。

    string authorizationEndpoint = 
          "https://www.linkedin.com/uas/oauth2/authorization" + 
           "?response_type=code" + 
           "&client_id=" + Uri.EscapeDataString(Options.AppId) + 
           "&redirect_uri=" + Uri.EscapeDataString(redirectUri) + 
           "&scope=" + Uri.EscapeDataString(scope) + 
           "&state=" + Uri.EscapeDataString(state); 
    
  3. LinkedInAuthenticationOptions類

    CallbackPath = new PathString("/signin-linkedin"); 
    

然後去Startup.Auth.cs文件App_Start文件夾中的項目,並在其中添加此代碼。

app.UseLinkedInAuthentication(
      APIKey: "...YourLinkedInAPIKeyHere...", 
      SecretKey: "...YourLinkedInSecretKeyHere..." 
      ); 

也向LinkedIn API提供您的應用程序詳細信息。你可以從這裏閱讀更多關於它的細節。

Using LinkedIn Authentication

這就是全部。祝你好運!

2

自從OP以來已經過去了一段時間,恕我直言,更直接的解決方案將用於Owin.Security.Providers NuGet package

將包裝添加到您的解決方案後,使用LinkedIn作爲提供商非常簡單,如the author blog的解釋。

而且,在這樣的事實,該項目是open sourced頂部,獎金就是你在同一時間進行其他認證供應商,如GitHub上,雅虎或StackExchange的支持:O)

相關問題