当我尝试使用Facebook或Google登录时,GetExternalLoginInfoAsync始终返回null

Ole*_*syk 8 c# asp.net-mvc owin asp.net-mvc-5

我的OWIN身份验证有问题.GetExternalLoginInfoAsync()当我尝试使用Facebook或Google登录时,我总是收到空值.

但是有一些神秘的案例..当我打开提琴手.我使用这种方法得到正确的数据.

我无法理解

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
Run Code Online (Sandbox Code Playgroud)

提前致谢!!

Ole*_*syk 7

我已经通过添加此代码解决了我的问题

context.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
Run Code Online (Sandbox Code Playgroud)

在课堂里:

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它解决了我的问题NULL.

注意:使用twitter授权登录时不要使用fiddler.你会收到错误.