Это для некоторых, кому может понадобиться справка о том, как решить проблему, в итоге я пошел по этому маршруту
- Удалите все формы безопасности для файла .svc
- Создайте messageInspector для добавления заголовка Basic Auth к WCF (клиенту)
- Добавьте messageInspector к ServiceBehavior
- Добавьте serviceBehavior к поведению конечной точки службы
- В службе создайтеa ServiceAuthorizationManager
- Добавьте ServiceAuthorizationManager в web.config вашего сервиса
1. Снимите любую защиту
<location path="Services/UpdaterService.svc">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
2.Создайте messageInspector для добавления заголовка Basic Auth в WCF (клиент)
public class ServiceMessageServiceCredentialsInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty requestMessageProperty = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
requestMessageProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
return null;
}
}
3.Добавьте MessageInspector к ServiceBehavior
public class ServiceInterceptionBehavior : BehaviorExtensionElement,IEndpointBehavior
{
public override System.Type BehaviorType
{
get { return typeof(ServiceInterceptionBehavior); }
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(new ServiceMessageServiceCredentialsInspector());
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}
public void Validate(ServiceEndpoint endpoint)
{
}
protected override object CreateBehavior()
{
throw new NotImplementedException();
}
}
4.Добавьте serviceBehavior к поведению вашей конечной точки сервиса
EndpointAddress address = new
EndpointAddress("http://localhost:14138/Services/Service.svc");
ChannelFactory<IService> myChannelFactory = new
ChannelFactory<IUpdaterService>(defaultBinding, address);
myChannelFactory.Endpoint.EndpointBehaviors.Add(new ServiceInterceptionBehavior());
var address2 = myChannelFactory.CreateChannel(address);
5.В Сервисе создайте ServiceAuthorizationManager
public class ServiceAuthorizationManager : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
//Extract the Athorizationm Header,a nd parse out the credentials converting to base64 string
var authHeader = WebOperationContext.Current.IncomingRequest.Headers["Authorization"];
if ((authHeader != null) && (authHeader != string.Empty))
{
var svcCredentials = System.Text.ASCIIEncoding.ASCII
.GetString(Convert.FromBase64String(authHeader.Substring(6)))
.Split(':');
return DefaultPasswordValidator.ValidateCridentials(svcCredentials[0], svcCredentials[1]);
}
else
{
//No authorization header was provided, so challenge the client to provide before proceeding:
WebOperationContext.Current.OutgoingResponse.Headers.Add("WWW-Authenticate: Basic realm=\"UpdaterService\"");
//Throw an exception with the associated HTTP status code equivalent to HTTP status 401
throw new FaultException("Please provide a username and password");
}
}
6.Добавьте ServiceAuthorizationManager в web.config вашего сервиса
<serviceAuthorization serviceAuthorizationManagerType="ServiceAuthorizationManager, AssemblyName, Version=2.0.0.1, Culture=neutral, PublicKeyToken=null" />
<serviceAuthenticationManager serviceAuthenticationManagerType="ServiceAuthenticationManager, AssemblyName"
authenticationSchemes="Basic" />