GetExternalLoginInfoAsync always return null when

2019-04-23 14:34发布

I have a problem with OWIN Authentication. I always receive null value from GetExternalLoginInfoAsync() when I trying log in using Facebook or Google.

BUT there is some mystical case.. When I open Fiddler. I get correct data using this method.

I can't get the reason

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

Thanks in advance!!

2条回答
淡お忘
2楼-- · 2019-04-23 15:15

You have to clear the Session before ExternalLoginCallback.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
    ControllerContext.HttpContext.Session.RemoveAll();

    // Request a redirect to the external login provider
    return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
查看更多
Luminary・发光体
3楼-- · 2019-04-23 15:41

I have solved my problem by adding this code

context.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;

in the class:

    private class ChallengeResult : HttpUnauthorizedResult
    {
        public ChallengeResult(string provider, string redirectUri)
            : this(provider, redirectUri, null)
        {
        }

        public ChallengeResult(string provider, string redirectUri, string userId)
        {
            LoginProvider = provider;
            RedirectUri = redirectUri;
            UserId = userId;
        }

        public string LoginProvider { get; set; }
        public string RedirectUri { get; set; }
        public string UserId { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // this line fixed the problem with returing null
            context.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;

            var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
            if (UserId != null)
            {
                properties.Dictionary[XsrfKey] = UserId;
            }
            context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
        }
    }

It fixed my problem with returning NULL.

Notice: don't use fiddler when logging with twitter authorization. You will receive error.

查看更多
登录 后发表回答