Search code examples
asp.net-mvcauthenticationoauthlinkedin-apiasp.net-mvc-5

LinkedIn Login for ASP.NET MVC 5 Application


I am working with an ASP.NET MVC 5 web application. I need to provide the users to login with their LinkedIn accounts. MVC 5 provides the support for Login with Facebook, Google. But I don't have a clear idea of how to implement this with LinkedIn.

There isn't any Katana support for LinkedIn in MVC 5. What would be the approach should I take to implement this specific behavior in MVC 5? Any suggestions would be greatly appreciated. Thanks.


Solution

  • Finally found a way to do it successfully..! I duplicate the code used for the implementation of Facebook authentication in Owin/Katana. You can find it here.

    Katana Project Source Code

    I copied all the files and the folder indicated inside the red box in the following picture.

    enter image description here

    Paste them in my own MVC 5 Web application project where I need to implement LinkedIn authentication. Renamed all the classes as LinkedIn instead of Facebook.

    enter image description here

    And then I changed the code to adapt it to LinkedIn.

    1. Constants Class Changed the DefaultAuthenticationType to LinkedIn instead of Facebook.
    2. LinkedInAuthenticationHandler class

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

      Change this code inside ApplyResponseChallengeAsync method.

      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 Class

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

    Then go to Startup.Auth.cs file in App_Start folder in your project and add this code in it.

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

    provide your application details to the LinkedIn API as well. You can read more details about it from here.

    Using LinkedIn Authentication

    That's all. Good luck!