LinkedIn登录ASP.NET MVC 5应用程序

Jak*_*ake 9 authentication asp.net-mvc oauth linkedin asp.net-mvc-5

我正在使用ASP.NET MVC 5Web应用程序.我需要提供用户登录他们的LinkedIn帐户.MVC 5为使用Facebook,Google登录提供支持.但我对如何使用LinkedIn实现这一点并不清楚.

在MVC 5中没有任何Katana对LinkedIn的支持.我应该采取什么方法来实现MVC 5中的这种特定行为?任何建议将不胜感激.谢谢.

Jak*_*ake 0

终于找到了成功的方法..!我复制了 Owin/Katana 中用于实现 Facebook 身份验证的代码。你可以在这里找到它。

Katana项目源代码

我复制了下图红框内的所有文件和文件夹。

在此输入图像描述

将它们粘贴到我自己的 MVC 5 Web 应用程序项目中,我需要在其中实现 LinkedIn 身份验证。将所有课程重命名为 LinkedIn,而不是 Facebook。

在此输入图像描述

然后我更改了代码以使其适应 LinkedIn。

  1. Constants 类将 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/~";
    
    Run Code Online (Sandbox Code Playgroud)

    在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);
    
    Run Code Online (Sandbox Code Playgroud)
  3. LinkedInAuthenticationOptions 类

    CallbackPath = new PathString("/signin-linkedin");
    
    Run Code Online (Sandbox Code Playgroud)

然后转到项目中 App_Start 文件夹中的 Startup.Auth.cs 文件,并在其中添加此代码。

    app.UseLinkedInAuthentication(
            APIKey: "...YourLinkedInAPIKeyHere...",
            SecretKey: "...YourLinkedInSecretKeyHere..."
            );
Run Code Online (Sandbox Code Playgroud)

还将您的申请详细信息提供给 LinkedIn API。您可以从此处阅读有关它的更多详细信息。

使用 LinkedIn 身份验证

就这样。祝你好运!