在ASP.NET Core 2.0中设置LinkedIn / OAuth身份验证

时间:2017-08-19 23:13:38

标签: oauth asp.net-core asp.net-authentication

我尝试将LinkedIn身份验证添加到我的ASP.NET Core 2.0应用程序中,但收到以下错误:

  

没有配置身份验证处理程序来处理该方案:LinkedIn

以下是我在ConfigureServices Startup.cs中添加LinkedIn / OAuth身份验证的方式:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
   .AddCookie("internal_cookie", options => {
         options.AccessDeniedPath = "/Account/Forbidden/";
         options.LoginPath = "/Account/Login";
    })
   .AddCookie("external_cookie")
   .AddOAuth("LinkedIn", options => {

         options.SignInScheme = "external_cookie";
         options.ClientId = "1234567890";
         options.ClientSecret = "1234567890";
         options.CallbackPath = "/linkedin-callback";

         options.AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization";
         options.TokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken";
         options.UserInformationEndpoint = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url,picture-urls::(original))";

         options.Scope.Add("r_basicprofile");
         options.Scope.Add("r_emailaddress");

         options.Events = new OAuthEvents
         {
             OnCreatingTicket = OnCreatingTicketLinkedInCallBack,
             OnTicketReceived = OnTicketReceivedCallback
         };
    })
    .AddFacebook(options =>
    {
          options.AppId = "1234567980";
          options.AppSecret = "1234567890";
          options.Events = new OAuthEvents
          {
             OnCreatingTicket = OnCreatingTicketFacebookCallback,
             OnTicketReceived = OnTicketReceivedCallback
          };
     })
     .AddGoogle(options =>
     {
           options.ClientId = "1234567890";
           options.ClientSecret = "1234567890";
           options.CallbackPath = "/google-callback";
           options.Events = new OAuthEvents
           {
              OnCreatingTicket = OnCreatingTicketGoogleCallback,
              OnTicketReceived = OnTicketReceivedCallback
           };
});

我的错误在哪里?

更新: 在建议更正后,我现在收到以下错误:

  

没有IAuthenticationSignInHandler配置为处理登录   该计划:social_login

1 个答案:

答案 0 :(得分:4)

您将与自定义LinkedIn处理程序注册关联的身份验证方案与OAuthHandler最终将调用以持久保存身份的登录方案(通常是Cookie处理程序实例)混合在一起。

修复您的注册,将LinkedIn指定为方案,将social_login指定为登录方案(假设您的Cookie处理程序确实命名为social_login),并且它应该有效:

services.AddAuthentication()
    .AddCookie("social_login")
    .AddOAuth("LinkedIn", options =>
    {
        options.SignInScheme = "social_login";

        // ...
    });

注意:如果SignInScheme是默认登录方案,则可以删除social_login分配(即,如果您致电services.AddAuthentication("social_login")services.AddAuthentication(options => options.DefaultSignInScheme = "social_login")

services.AddAuthentication("social_login")
    .AddCookie("social_login")
    .AddOAuth("LinkedIn", options =>
    {
        // ...
    });