En este post te voy a mostrar como puedes especificar el UserName y el Password en un cliente proxy Wcf resuelto con Autofac.
Ejemplo del Binding en la Configuración.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<basicHttpsBinding> | |
<binding name="My_BindingConfiguration" > | |
<security mode="TransportWithMessageCredential"> | |
<message clientCredentialType="UserName" /> | |
</security> | |
</binding> | |
</basicHttpsBinding> |
Ejemplo de resolución de ClientChannel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class ContainerExtensions | |
{ | |
// Composicion de cliente Proxy | |
public static ContainerBuilder ComposeChannelFactory<T>(this ContainerBuilder builder, | |
[NotNull] IBindingConsultaLco binding, | |
UserCredentials credentials) | |
{ | |
if (builder == null) throw new ArgumentNullException(nameof(builder)); | |
if (binding == null) throw new ArgumentNullException(nameof(binding)); | |
builder | |
.Register(c => | |
{ | |
var basicHttpsBinding = new BasicHttpsBinding(BasicHttpsSecurityMode.TransportWithMessageCredential) | |
{ | |
MaxBufferPoolSize = binding.MaxBufferPoolSize, | |
MaxReceivedMessageSize = binding.MaxReceivedMessageSize, | |
MaxBufferSize = (int) binding.MaxBufferSize, | |
ReceiveTimeout = binding.ReceiveTimeout, | |
SendTimeout = binding.SendTimeout, | |
OpenTimeout = binding.OpenTimeout, | |
CloseTimeout = binding.CloseTimeout | |
}; | |
basicHttpsBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName; | |
return new ChannelFactory<T>( | |
basicHttpsBinding, | |
new EndpointAddress(binding.EndPoint)); | |
}) | |
// Las credenciales no se puede modificar despues de que el ClientChannel fue creado. | |
// es necesario hacer la asignacion antes. | |
.OnActivating(c=> | |
{ | |
c.Instance.Credentials.UserName.UserName = credentials.UserName; | |
c.Instance.Credentials.UserName.Password = credentials.Password; | |
}) | |
.SingleInstance(); | |
return builder; | |
} | |
public static ContainerBuilder ComposeChannel<T>(this ContainerBuilder builder) | |
{ | |
if (builder == null) throw new ArgumentNullException(nameof(builder)); | |
builder.Register(c => c.Resolve<ChannelFactory<T>>().CreateChannel()) | |
.As<T>() | |
.UseWcfSafeRelease(); | |
return builder; | |
} | |
} |
Observaciones.
El ejemplo anterior puede no soportar múltiples credenciales de usuario. Si requieres tener más control, te recomiendo que hagas la creación de las ChannelFactory y los ClientChannel en un componente aparte, sin Autofac. Con esos componentes creados puedes hacer la inyección de dependencias.
Últimas entradas de Gustavo Sánchez (ver todo)
- NVL in SQL Server - 2023-11-01
- ¿Que es Cake Build? - 2023-02-22
- #How to fix error: MSB4019: The imported project «Microsoft.Data.Tools.Schema.SqlTasks.targets» was not found - 2023-02-20