Web Services 跳过BeforeSendRequest中的标头验证

hsvhsicv  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(134)

根据this主题我正在使用这段代码

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request,  System.ServiceModel.IClientChannel channel)
{
    HttpRequestMessageProperty httpRequestMessage;
    object httpRequestMessageObject;
    if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
    {
        httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
        if (string.IsNullOrEmpty(httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER]))
        {
            httpRequestMessage.Headers[USER_AGENT_HTTP_HEADER] = this.m_userAgent;
        }
    }
    else
    {
        httpRequestMessage = new HttpRequestMessageProperty();
        requestMessageProperty.Headers.Add("Authorization",  token);
        request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
    }

但不幸是,自从我将代码从. netframwork4.7移到. netcore2.1项目后,我得到了以下错误代码:
未能将值为“qqqqqqqqq=”的HTTP标头“Authorization”复制到“System.Net.Http.Headers. HttpRequestHeaders”。\r\n值“qqqqqqqqq=”的格式无效。
在旧项目中,一切正常,是否可以跳过授权标题验证?谢谢

pw9qyyiw

pw9qyyiw1#

.net核心验证受权信头格式。根据https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization,受权信头的格式应为:
Authorization: <type> <credentials>
如果授权头在typecredentials之间不包含空格或包含多个空格,则会出现此错误
这将很好地工作:

requestMessageProperty.Headers.Add("Authorization", $"my-custom-auth-type {token}");

相关问题