简体   繁体   English

WCF。 设置用户凭证

[英]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 我有wcf services项目和asp.net mvc项目,我正在使用ninject wcf客户端扩展来注入服务,例如本示例

 Bind<IService>().ToServiceChannel();

Now i'm I need to add authentication and authorization for wcf services. 现在,我需要为wcf服务添加身份验证和授权。 I have implemented UserNamePasswordValidator and IAuthorizationPolicy . 我已经实现了UserNamePasswordValidatorIAuthorizationPolicy

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: 您需要创建一个channelFactory来设置用户凭据:

Example : 范例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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