繁体   English   中英

Instagram OAuth GetExternalLoginInfoAsync在.NET Core 2.0中始终返回null

[英]Instagram OAuth GetExternalLoginInfoAsync always returns null in .NET Core 2.0

我可以使用此库在.NET Core 1中配置Instagram身份验证,但似乎无法使其在.NET Core 2中运行。

似乎没有与.NET Core 2.0兼容的Instagram OAuth库,因此,我尝试使用内置的AddOAuth方法。 我能够毫无问题地进行所有身份验证,但是由于某种原因,LinkLoginCallback中的GetExternalLoginInfoAsync方法始终返回null。 由于所有这些事情都是在AspNetCore.Authentication中发生的,因此我假设我的设置和配置不正确(或缺少一些关键内容),或者由于其他原因该解决方案无法正常工作。

我在ConfigureServices中的配置如下所示:

services.AddAuthentication().AddOAuth("Instagram", "Instagram", options =>
        {
            options.ClientId = "MYID";
            options.ClientSecret = "MYSECRET";
            options.AuthorizationEndpoint = "https://api.instagram.com/oauth/authorize/";
            options.CallbackPath = "/signin-instagram";
            options.TokenEndpoint = "https://api.instagram.com/oauth/access_token";
            options.Scope.Add("basic");
            options.ClaimsIssuer = "Instagram";
            options.UserInformationEndpoint = "https://api.instagram.com/v1/users/self";
        });

有人能够在.NET Core 2.0中成功通过Instagram进行身份验证吗? 任何帮助将不胜感激!

不是专门针对Instagram,但我已经使用上述类似的设置为Twitch使用了自定义OAuth验证。

我从GetExternalLoginInfoAsync收到了相同的Null错误,我必须经过许多步骤才能使其工作。

最后,我通过查看此处找到的GetExternalLoginInfoAsync方法的源代码找到了问题-Microsoft.AspNetCore.Identity.SignInManager.cs

  1. 我发现的第一件事实际上是方法的第一行

     var auth = await Context.AuthenticateAsync(IdentityConstants.ExternalScheme); 

    该方法设置为始终使用ExternalScheme常量,因此,如果在Startup.cs中设置自定义SignInScheme,则在调用此方法时将不会使用该常量。 将SignInScheme设置为默认值似乎对我也不起作用。

  2. LoginProviderKey为空。

     if (auth?.Principal == null || items==null||!items.ContainsKey(LoginProviderKey)){return null;} 
  3. 解决上述问题后,我发现没有设置任何索偿要求,因此我也必须手动进行设置。 我在另一个Stackoverflow问题上找到了一个示例-AddOAuthlinkedin dotnet core 2.0

请在下面查看我的最终代码:

Startup.cs配置OAuth中间件

在启动中,我将SignInScheme设置为使用IdentityConstants.ExternalScheme值,并在OnCreatingTicket中添加了客户Claims设置事件。

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddOAuth("Twitch", "Twitch", o =>
              {
                  o.SignInScheme = IdentityConstants.ExternalScheme;
                  o.ClientId = "MY CLIENT ID";
                  o.ClientSecret = "MY CLIENT SECRET";
                  o.CallbackPath = "/signin-twitch";
                  o.ClaimsIssuer = "Twitch";
                  o.AuthorizationEndpoint = "https://api.twitch.tv/kraken/oauth2/authorize";
                  o.TokenEndpoint = "https://api.twitch.tv/api/oauth2/token";
                  o.UserInformationEndpoint = "https://api.twitch.tv/helix/users";
                  o.Scope.Add("openid");
                  o.Scope.Add("user:read:email");

                  o.Events = new Microsoft.AspNetCore.Authentication.OAuth.OAuthEvents
                  {
                      OnCreatingTicket = async context =>
                      {
                          var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                          request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
                          request.Headers.Add("x-li-format", "json");

                          var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted);
                          response.EnsureSuccessStatusCode();
                          var user = JObject.Parse(await response.Content.ReadAsStringAsync());

                          var data = user.SelectToken("data")[0];

                          var userId = (string)data["id"];
                          if (!string.IsNullOrEmpty(userId))
                          {
                              context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userId, ClaimValueTypes.String, context.Options.ClaimsIssuer));
                          }

                          var formattedName = (string)data["display_name"];
                          if (!string.IsNullOrEmpty(formattedName))
                          {
                              context.Identity.AddClaim(new Claim(ClaimTypes.Name, formattedName, ClaimValueTypes.String, context.Options.ClaimsIssuer));
                          }

                          var email = (string)data["email"];
                          if (!string.IsNullOrEmpty(email))
                          {
                              context.Identity.AddClaim(new Claim(ClaimTypes.Email, email, ClaimValueTypes.String,
                                  context.Options.ClaimsIssuer));
                          }
                          var pictureUrl = (string)data["profile_image_url"];
                          if (!string.IsNullOrEmpty(pictureUrl))
                          {
                              context.Identity.AddClaim(new Claim("profile-picture", pictureUrl, ClaimValueTypes.String,
                                  context.Options.ClaimsIssuer));
                          }
                      }
                  };
              });

AccountController.cs创建挑战

发出挑战时,我们还必须包含LoginProvider值。

public IActionResult LoginWithTwich(string returnUrl = null)
    {

        var authProperties = _signInManager.ConfigureExternalAuthenticationProperties("Twitch", returnUrl);

        return Challenge(authProperties, "Twitch");
    }

AccountController.cs处理回调

最后,当我们处理回调时,GetExternalLoginInfoAsync方法不再返回null。

public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
    {
        ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync();
        //to sign the user in if there's a local account associated to the login provider
        var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
        if (!result.Succeeded)
        {
            return RedirectToAction("ConfirmTwitchLogin", new { ReturnUrl = returnUrl });
        }
        if (string.IsNullOrEmpty(returnUrl))
        {
            return Redirect("~/");
        }
        else
        {
            return RedirectToLocal(returnUrl);
        }
    }

为我工作(版本网络核心2.2)

  1. 安装下一个软件包:

安装包AspNet.Security.OAuth.Instagram-版本2.0.1

  1. 注册客户端密钥后,例如用户秘密

dotnet用户秘密设置Authentication:Instagram:ClientId YourClientId

dotnet用户秘密设置身份验证:Instagram:ClientSecret YourClientSecret

最后在启动类中:

services.AddAuthentication()
    .AddInstagram(instagramOptions =>
                {
                    instagramOptions.ClientId = Configuration["Authentication:Instagram:ClientId"];
                    instagramOptions.ClientSecret = Configuration["Authentication:Instagram:ClientSecret"];
                }
    );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM