简体   繁体   English

ASP.NET Web API Authentication.GetExternalLoginInfoAsync 始终返回 null

[英]ASP.NET Web API Authentication.GetExternalLoginInfoAsync always return null

I have ASP.NET 5 project and I am using Web API to establish the external login (for Facebook and Google).我有 ASP.NET 5 项目,我正在使用 Web API 来建立外部登录(用于 Facebook 和 Google)。 In my case, I have Web API controller (Not MVC controller) which contains the following code :就我而言,我有包含以下代码的 Web API 控制器(不是 MVC 控制器):

[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
    if (error != null)
        return Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error));

    if (!User.Identity.IsAuthenticated)
        return new ChallengeResult(provider, this);

    var exLog = await Authentication.GetExternalLoginInfoAsync();

when the mobile application calling this method, I am getting the request authenticated correctly and the property User.Identity.IsAuthenticated is true, and I can see in the debugger, that the user is correct, but the problem is the exlog variable is always coming null.当移动应用程序调用此方法时,我正确验证了请求并且属性User.Identity.IsAuthenticated为真,我可以在调试器中看到用户是正确的,但问题是exlog变量总是出现空值。

here is the Authentication property这是Authentication属性

private IAuthenticationManager Authentication => this.Request.GetOwinContext().Authentication;

I read every question on the stack about this bug, but nothing helped me, most of the questions are for the MVC controller, so that did not help.我阅读了堆栈上有关此错误的每个问题,但没有任何帮助,大多数问题都是针对 MVC 控制器的,因此没有帮助。

for example, this question did not help me because it is for MVC.例如,这个问题对我没有帮助,因为它是针对 MVC 的。

Update I am using Owin 4.0更新我正在使用 Owin 4.0

For all the people who asked me in the comments to put my answer.对于所有在评论中要求我回答的人。
Actually, the problem has not been solved and I did not find a solution for this,其实这个问题还没有解决,我也没有找到解决办法,
I am sorry to say that.我很抱歉这么说。

What I discovered is that I do not need to call this method from the beginning.我发现我不需要从一开始就调用这个方法。
and I went completely another way (I do not remember how and why)我走了完全不同的路(我不记得如何以及为什么)

Just small advice: consider the situation again and reconsider if you really need to call this method.只是一个小建议:再次考虑这种情况,并重新考虑是否真的需要调用这个方法。

I wish I had been more helpful, I am sorry.我希望我能提供更多帮助,我很抱歉。

You are getting "NULL" because you didn't write the code if the IsAuthenticated = true then what should it do?如果 IsAuthenticated = true,您将收到“NULL”,因为您没有编写代码,那么它应该怎么做? (means redirection to any page or else). (意味着重定向到任何页面或其他)。

see carefully your if condition for IsAuthenticated: if ( !User.Identity.IsAuthenticated )仔细查看 IsAuthenticated 的 if 条件: if ( !User.Identity.IsAuthenticated )

May be this can help you.也许这可以帮助你。

Try to enable Google + API in the Google console.尝试在 Google 控制台中启用 Google + API。 Also make sure the secret key is not concatenated with a space at the end after you paste it to your code.还要确保在将密钥粘贴到代码后,密钥没有在末尾连接一个空格。

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

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