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

标签 c# asp.net-mvc asp.net-mvc-5 owin

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

但是有一些神秘的情况。当我打开 Fiddler 时。我使用这种方法获得了正确的数据。

找不到原因

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

提前致谢!!

最佳答案

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

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

在类里面:

    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);
        }
    }

它解决了我返回 NULL 的问题。

注意:使用 twitter 授权登录时不要使用 fiddler。您将收到错误消息。

关于c# - 当我尝试使用 Facebook 或 Google 登录时,GetExternalLoginInfoAsync 始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25402008/

相关文章:

asp.net-mvc-5 - 使用 AuthorizeAttribute 或 IAuthorizationFilter 有什么区别?

c# - 如何使用 C# 在 WPF 中开发类似 Google Chrome 浏览器的应用程序

c# - EF 失败将 View 映射到对象

c# - 如何禁用 ItemsSource 与 ComboBox 的 Text 属性同步

c# - "Column out of Range"填充 Excel 工作表时出错

css - 如何让 ASP.NET MVC 导航栏水平而不是垂直?

c# - 如何在 MVC 3 中使用模型优先方法验证搜索字符串?

c# - 我应该使用什么 SSL URL 来进行 Azure 门户身份验证

forms-authentication - 授权时 ASP.NET MVC5 应用程序抛出 NullReferenceException

c# - ASP.NET MVC 5 - 重定向到具有文化的同一页面