在 ASP.NET Core 2.0 中设置 LinkedIn/OAuth 身份验证 [英] Setting Up LinkedIn/OAuth Authentication in ASP.NET Core 2.0

查看:15
本文介绍了在 ASP.NET Core 2.0 中设置 LinkedIn/OAuth 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

No authentication handler is configured to handle the scheme: LinkedIn

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

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

我的错误在哪里?

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

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

没有配置 IAuthenticationSignInHandler 来处理登录方案:social_login

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

推荐答案

您将与自定义 LinkedIn 处理程序注册关联的身份验证方案与 OAuthHandler 最终将调用以持久化的登录方案混淆身份(通常是 cookie 处理程序实例).

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).

修复您的注册以将 LinkedIn 指定为方案并将 social_login 指定为登录方案(假设您的 cookie 处理程序确实名为 social_login),它应该可以工作:

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";

        // ...
    });

注意:如果 social_login 是默认登录方案(即,如果您调用 services.AddAuthentication("social_login")),您可以删除 SignInScheme 分配services.AddAuthentication(options => options.DefaultSignInScheme = "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 =>
    {
        // ...
    });

这篇关于在 ASP.NET Core 2.0 中设置 LinkedIn/OAuth 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆