WCF。设置用户凭据

WCF. Set user credentials

对不起,我的英语不好

我有 wcf 服务项目和 asp.net mvc 项目 我正在使用 ninject wcf 客户端扩展来注入服务,就像这个例子

 Bind<IService>().ToServiceChannel();

现在我需要为 wcf 服务添加身份验证和授权。我已经实现了 UserNamePasswordValidatorIAuthorizationPolicy.

在所有示例中使用的服务引用和用户凭据如下添加:

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

但我没有创建服务参考。我只有接口。并像这里一样使用它:

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

如何添加用户凭据?

您需要为您创建一个 channelFactory 来设置用户凭据:

示例:

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