我正在使用dotnet core,希望在我的网站上设置LinkedIn身份验证。由于没有像Facebook、Google和Twitter那样的默认认证生成器,因此我决定使用以下通用实现:
services.AddAuthentication().AddOAuth("LinkedIn",
c =>
{
c.ClientId = Configuration["linkedin-app-id"];
c.ClientSecret = Configuration["linkedin-app-secret"];
c.Scope.Add("r_basicprofile");
c.Scope.Add("r_emailaddress");
c.CallbackPath = "/signin-linkedin";
c.AuthorizationEndpoint = "https://www.linkedin.com/oauth/v2/authorization";
c.TokenEndpoint = "https://www.linkedin.com/oauth/v2/accessToken";
c.UserInformationEndpoint = "https://api.linkedin.com/v1/people/~:(id,formatted-name,email-address,picture-url)";
})
我遇到了一个问题,因为GetExternalLoginInfoAsync()为空,查看Identity ASP.net core源代码,发现是由于providerkey为空导致的。
以下是来自asp.net core代码:
var providerKey = auth.Principal.FindFirstValue(ClaimTypes.NameIdentifier);
var provider = items["LoginProvider"] as string;
if (providerKey == null || provider == null)
{
return null;
}
问题是在LinkedIn声明中,我该在哪里添加ClaimTypes.NameIdentifier?