响应内容长度不匹配:写入的字节太少

问题描述 投票:0回答:1

我的ASP.NET Core应用程序使用“现成的”外部登录身份验证。我要实现的-在Facebook挑战中,我想包装重定向URL并将其作为json返回以在jquery前端中使用。但是请求结束后,我在浏览器中看到500错误,在应用程序控制台中看到下一个错误:

失败:Microsoft.AspNetCore.Server.Kestrel [13]连接ID“ 0HLV651D6KVJC”,请求ID“ 0HLV651D6KVJC:00000005”:客户端引发了未处理的异常应用。 System.InvalidOperationException:响应内容长度不匹配:写入的字节太少(470个中的0个)。

我的外部登录操作,没什么特别的]]

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public IActionResult ExternalLogin(string provider, string returnUrl = null)
{
    // Request a redirect to the external login provider.
    var redirectUrl = Url.Action(nameof(ExternalLoginCallback), "Account", new { returnUrl });
    var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
    return Challenge(properties, provider);
}

Facebook身份验证配置:

services.AddAuthentication().AddFacebook(facebookOptions =>
{
    facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
    facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];

    facebookOptions.Events.OnRedirectToAuthorizationEndpoint =
        async (x) =>
        {

            UTF8Encoding encoding = new UTF8Encoding();
            var content = JsonConvert.SerializeObject(new { redirect_url = x.RedirectUri });
            byte[] bytes = encoding.GetBytes(content);

            x.Response.StatusCode = 200;
            x.Response.ContentLength = bytes.Length;
            x.Response.ContentType = "text/plain";
            x.Response.Body = new MemoryStream();

            await x.Response.WriteAsync(content);
            // at this point I see that x.Response.Body.Length == 470, but message states there are 0 of 470 written
        };
});

有什么办法可以使它起作用?

我的ASP.NET Core应用程序使用“现成的”外部登录身份验证。我要实现的-在Facebook挑战中,我想包装重定向URL并将其作为json返回以在jquery前端中使用。 ...

c# asp.net-core kestrel-http-server asp.net-authentication
1个回答
0
投票

已更改代码以写入原始响应流,并且现在可以使用。

© www.soinside.com 2019 - 2024. All rights reserved.