Search code examples
c#asp.netwcfninjectninject-extensions

WCF. Set user credentials


Sorry, my english is bad.

I have wcf services project and asp.net mvc project I'm using ninject wcf client extension to inject services like in this example

 Bind<IService>().ToServiceChannel();

Now i'm I need to add authentication and authorization for wcf services. I have implemented UserNamePasswordValidator and IAuthorizationPolicy.

In all examples used service reference and user credentials added like here:

ServiceClient host = new ServiceClient();
host.ClientCredentials.UserName.UserName = "user";
host.ClientCredentials.UserName.Password = "pass";

But i didn't create service reference. I have only interfaces. And use it like here:

public class HomeController : Controller
{
    private readonly IService _service;
    public HomeController(IService service)
    {
        _service = service;    
    }
}

How I can add user credentials?


Solution

  • You need to create a channelFactory for you to set the user credentials:

    Example :

    var result = new ChannelFactory<IService>("*", new EndpointAddress(serviceAddress));
        result.Credentials.UserName.UserName = "Username";
        result.Credentials.UserName.Password = "password";