Best practice for setting username/password using WcfIntegration

176 views
Skip to first unread message

ohaga

unread,
Jan 27, 2010, 1:19:40 AM1/27/10
to Castle Project Users
I am working on a project where we are using WcfIntegration to create
client-proxies. Typical code is shown i text below:

public void Init()
{
var container = new WindsorContainer();
container.AddFacility<WcfFacility>();
WSHttpBinding binding;
binding = new WSHttpBinding
(SecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType =
MessageCredentialType.UserName;

var endpoint = WcfEndpoint.BoundTo(binding).At("http://localhost/
myservice.svc");
var defaultClientModel = new DefaultClientModel()
{
Endpoint = endpoint
};
var s = typeof (IMyService);
container.Register(Component.For(s).ActAs(defaultClientModel));
}

I want to add something like this code:
var credentials = new ClientCredentials();
credentials.UserName.UserName = "username";
credentials.UserName.Password = "password";
defaultClientModel.AddExtensions(credentials);

But I get an error when the channel is created (credentials already
added).

A possible workaround (hack) is to change the code in
WcfExplicitExtension.Install(ServiceEndpoint endpoint, bool
withContract, IKernel kernel, IWcfBurden burden)
{
object extension = GetExtensionInstance(kernel, burden);
if (extension is ClientCredentials)
{
var config = (ClientCredentials)extension;
var credentials = endpoint.Behaviors.Find<ClientCredentials>();
credentials.UserName.Password = config.UserName.Password;
credentials.UserName.UserName = config.UserName.UserName;
}
else if (extension is IEndpointBehavior)
{
endpoint.Behaviors.Add((IEndpointBehavior)extension);
}

Could we use another approach to achieve the same thing?

Craig Neuwirt

unread,
Jan 27, 2010, 8:48:19 AM1/27/10
to castle-pro...@googlegroups.com
You should register and IChannelFactoryAware class in the container
e.g

        public class UserNameClientCredentials : AbstractChannelFactoryAware
        {
                public string UserName { get; set; }

                public string Password { get; set; }

                public override void Created(ChannelFactory channelFactory)
                {
                        channelFactory.Credentials.UserName.UserName = UserName;
                        channelFactory.Credentials.UserName.Password = Password;
                }
        }



--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


ohaga

unread,
Jan 28, 2010, 2:03:33 AM1/28/10
to Castle Project Users
AbstractChannelFactoryAware worked like a charm!!

Thanks.

> > castle-project-u...@googlegroups.com<castle-project-users%2Bun subs...@googlegroups.com>

Manuel Anaya

unread,
Aug 26, 2013, 5:03:57 AM8/26/13
to castle-pro...@googlegroups.com
Hello, I know it's been a while, but do you happen to have a code snippet of how did you get this working? I have tried to ask in a new thread but haven't got any answers at all. 

Best regards.
Reply all
Reply to author
Forward
0 new messages