我正在尝试使用带有奇怪问题的基本身份验证的REST服务。第一个请求不包括指定的基本身份验证凭据。每发出第一个请求都会发生这种情况每次应用程序启动时都会发生。后续请求不会发生这种情况。我使用ClientBase<T>
类来调用API,它看起来像这样:
public class MyApi : ClientBase<IMyApi>
{
protected override IMyApi CreateChannel()
{
//Remove all client credentials.
ChannelFactory.Endpoint.Behaviors.RemoveAll<ClientCredentials>();
//Create new client credentials and add to the endpoint behaviours.
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = Username;
clientCredentials.UserName.Password = Password;
ChannelFactory.Endpoint.Behaviors.Add(clientCredentials);
return base.CreateChannel();
}
public MyApi() : base (DefaultEndpointName)
{}
}
我的绑定设置如下:
<customBinding>
<binding name="myApiBinding">
<webMessageEncoding webContentTypeMapperType="ContentTypeMapper,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<httpTransport manualAddressing="true" maxReceivedMessageSize="6553600" authenticationScheme="Basic" realm="myRealm" />
</binding>
</customBinding>
我尝试将项目提升到.Net 4,在初始化API后添加客户端凭据,但似乎都没有达到预期的效果。
有没有办法为每个请求添加凭据?
答案 0 :(得分:1)