根据这个主题,我正在使用这个代码块
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);
}
但不幸的是,由于我将代码从 .net framwork 4.7 移动到 .net Core 2.1 项目,因此我收到此错误代码:
无法复制带有值的 HTTP 标头"授权" 'qqqqqq=' to 'System.Net.Http.Headers.HttpRequestHeaders'.\r值的格式 'qqqqqq=' 无效。
在旧项目中一切正常 是否可以跳过授权标头验证? 谢谢
.net core 验证授权标头格式。 根据 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization 授权标头的格式应为:Authorization: <type> <credentials>
如果授权标头在类型和凭据之间不包含空格或包含多个空格,则会收到此错误
这将正常工作:
requestMessageProperty.Headers.Add("Authorization", $"my-custom-auth-type {token}");