Setting Up LinkedIn/OAuth Authentication in ASP.NET Core 2.0

回眸只為那壹抹淺笑 提交于 2019-12-18 17:07:19

问题


I'm trying to add LinkedIn authentication to my ASP.NET Core 2.0 app but getting the following error:

No authentication handler is configured to handle the scheme: LinkedIn

Here's how I add LinkedIn/OAuth authentication in the ConfigureServices in Startup.cs:

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
           };
});

Where's my error?

UPDATE: After making the corrections suggested, I'm now getting the following error:

No IAuthenticationSignInHandler is configured to handle sign in for the scheme: social_login


回答1:


You mixed up the authentication scheme associated with your custom LinkedIn handler registration with the sign-in scheme that OAuthHandler will ultimately call to persist the identity (typically a cookie handler instance).

Fix your registration to specify LinkedIn as the scheme and social_login as the sign-in scheme (assuming your cookie handler is indeed named social_login), and it should work:

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

        // ...
    });

Note: you can remove the SignInScheme assignation if social_login is the default sign-in scheme (i.e if you call services.AddAuthentication("social_login") or services.AddAuthentication(options => options.DefaultSignInScheme = "social_login"):

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


来源:https://stackoverflow.com/questions/45777071/setting-up-linkedin-oauth-authentication-in-asp-net-core-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!