简体   繁体   中英

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). In my case, I have Web API controller (Not MVC controller) which contains the following code :

[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.

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.

for example, this question did not help me because it is for MVC.

Update I am using 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? (means redirection to any page or else).

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

May be this can help you.

Try to enable Google + API in the Google console. Also make sure the secret key is not concatenated with a space at the end after you paste it to your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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