![](/img/trans.png)
[英]GetExternalLoginInfoAsync returns null in asp.net core 2.0
[英]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
我發現的第一件事實際上是方法的第一行
var auth = await Context.AuthenticateAsync(IdentityConstants.ExternalScheme);
該方法設置為始終使用ExternalScheme常量,因此,如果在Startup.cs中設置自定義SignInScheme,則在調用此方法時將不會使用該常量。 將SignInScheme設置為默認值似乎對我也不起作用。
LoginProviderKey為空。
if (auth?.Principal == null || items==null||!items.ContainsKey(LoginProviderKey)){return null;}
解決上述問題后,我發現沒有設置任何索償要求,因此我也必須手動進行設置。 我在另一個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)
安裝包AspNet.Security.OAuth.Instagram-版本2.0.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.