ASP。净Web API认证。GetExternalLoginInfoAsync总是返回零

时间:2022-01-01 19:25:00

I have ASP.NET 5 project and I am using Web API to establish the external login (for Facebook and Google). In my case, I have Web API controller (Not MVC controller) which contains the following code :

我有ASP。NET 5项目和我使用Web API来建立外部登录(对于Facebook和谷歌)。在我的例子中,我有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.

当移动应用程序调用此方法时,我将正确地获得请求身份验证和属性用户. identity。IsAuthenticated是正确的,我可以在调试器中看到,用户是正确的,但问题是exlog变量总是null。

here is the Authentication property

这是身份验证属性

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

1 个解决方案

#1


0  

You are getting "NULL" because you didn't write the code if the IsAuthenticated = true then what should it do? (means redirection to any page or else).

您将得到“NULL”,因为您没有编写代码,如果IsAuthenticated = true,那么它应该做什么?(指重定向到任何页面或其他)。

see carefully your if condition for IsAuthenticated: if (!User.Identity.IsAuthenticated)

仔细查看IsAuthenticated的if条件:if (!User.Identity.IsAuthenticated)

May be this can help you.

也许这能帮助你。

#1


0  

You are getting "NULL" because you didn't write the code if the IsAuthenticated = true then what should it do? (means redirection to any page or else).

您将得到“NULL”,因为您没有编写代码,如果IsAuthenticated = true,那么它应该做什么?(指重定向到任何页面或其他)。

see carefully your if condition for IsAuthenticated: if (!User.Identity.IsAuthenticated)

仔细查看IsAuthenticated的if条件:if (!User.Identity.IsAuthenticated)

May be this can help you.

也许这能帮助你。