简体   繁体   中英

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?

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM