15

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

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
  • I'll add this as a comment to start with as it might not be relevant. I had an issue in the past where the `Auth..` data wasn't available on the returning request but simply redirecting to a `AuthBounce` Action (e.g. `return Redirect("~/AuthBounce");` _then_ trying to access `var exLog = await Authentication.GetExternalLoginInfoAsync();` worked. Give that a try. I appreciate this might not be _exactly_ the same as your setup looks a little different. – scgough May 30 '18 at 12:48
  • where should I put `return Redirect("~/AuthBounce");` ? – Hakan Fıstık May 31 '18 at 07:02
  • 1
    remove the current `var exLog = await Authentication.GetExternalLoginInfoAsync();` and replace it with the redirect. On your new `AuthBounce` action add in the `var exLog = await Authentication.GetExternalLoginInfoAsync();` and see if it is no longer null. Like I say, this might not be relevant/work but worth a try – scgough May 31 '18 at 07:42
  • @scgough thank you I tried it but did not work – Hakan Fıstık May 31 '18 at 09:10
  • **Important after putting this question and start a bounty for it, I discovered that I do not need to call this method at all and my problem was solved, it was completely related to another thing, then this problem, Sorry for Inconvenience** – Hakan Fıstık Jun 07 '18 at 11:21
  • @HakamFostok : Do you mind posting the solution here? – Dio Phung Jun 09 '18 at 00:18
  • @HakamFostok Please post the solution – Juan Mitchell Aug 27 '18 at 03:04
  • @user1431866 Actually I changed my way completely, The solution for me that I should not call this method in the first place. According to my situation, I should not call this method at all. – Hakan Fıstık Aug 27 '18 at 08:08

3 Answers3

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.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
-1

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.

NMehta
  • 56
  • 4
-1

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.

Demetraux
  • 1
  • 2