简体   繁体   English

在集成测试中测试自托管WCF服务的身份验证

[英]Testing Authentication For Self-Hosted WCF Service in an Integration Test

I am trying to consume a self-hosted WCF application using SSL and a custom authentication validator from within an integration test. 我正在尝试从集成测试中使用SSL和自定义身份验证器使用自托管WCF应用程序。 So far I am able to self-host the service but I am not able to figure out how to consume it. 到目前为止,我可以自行托管该服务,但无法弄清楚如何使用它。

Here is the self-hosting code (it is not dependent on Web.Config, as far as I know): 这是自托管代码(据我所知,它不依赖于Web.Config):

[ClassInitialize]
public static void TestClassInitialize(TestContext testContext)
{
    const string serviceAddress = "https://localhost/SelfHostedService";
    Uri _svcEndpointUri = new Uri(serviceAddress);
    var binding = new WSHttpBinding
    {
        Security =
        {
            Mode = SecurityMode.TransportWithMessageCredential,
            Message = {ClientCredentialType = MessageCredentialType.UserName}
        }
    };
    ServiceDebugBehavior debugBehavior = new ServiceDebugBehavior
    {
        IncludeExceptionDetailInFaults = true
    };
    MyServiceApi _api = new MyServiceApi();
    ServiceHost _svcHost = new ServiceHost(_api, _svcEndpointUri);
    _svcHost.Description.Behaviors.Remove<ServiceDebugBehavior>();
    _svcHost.Description.Behaviors.Add(debugBehavior);

    // Ensure that SSL certificate & authentication interceptor get used
    ServiceCredentials credentials = new ServiceCredentials();
    credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
    credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new MyCustomAuthenticationValidator();
    credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "SubjectName");
    _svcHost.Description.Behaviors.Remove<ServiceCredentials>();
    _svcHost.Description.Behaviors.Add(credentials);

    // Add IUbiquity and mex endpoints
    Uri endpointAddress = new Uri(serviceAddress + "/UbiquityApi.svc");
    _svcHost.AddServiceEndpoint(typeof (IUbiquityApi), binding, endpointAddress);


    // Specify InstanceContextMode, which is required to self-host
    var behavior = _svcHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
    behavior.InstanceContextMode = InstanceContextMode.Single;
    _svcHost.Open();
}

What I'd like to be able to do looks like this, but I have no idea how I'd go about accomplish this: 我想做的事情看起来像这样,但是我不知道如何去实现这一点:

[TestMethod]
public void TestAuthentication(){
    var api = _svcHost.MagicallyRetrieveServiceInstance();
    api.Credentials = new MagicCredentials("my username", "my password");
    Assert.AreEqual(3, api.AddNumbers(1,2));
    // Also assert that I am authenticated 

    api.Credentials = new MagicCredentials("my username", "my password");
    bool exceptionWasThrown = false;
    try {
       api.AddNumbers(1,2);
    }
    catch(NotLoggedInException l){ // or something
       exceptionWasThrown = true;
    }
    Assert.IsTrue(exceptionWasThrown);
}

My ideal solution would allow me to retrieve the service contract from the service host, and allow me to set the credentials used for the service contract. 我理想的解决方案将允许我从服务主机检索服务合同,并允许我设置用于服务合同的凭据。 I should only have to supply the credentials once to the service contract, and then I should be able to call methods directly, as if I were communicating over the wire (thus making this an integration test). 我只需要向服务合同提供一次凭据,然后就应该能够直接调用方法,就像通过有线进行通信一样(因此使它成为集成测试)。 How should I go about this? 我应该怎么做?

To consume the web service, simply add the service as a service reference, and then use the service reference client. 要使用Web服务,只需将服务添加为服务引用,然后使用服务引用客户端即可。

Done right, this will take care of the bindings needed for authentication, effectively putting the WCF configurations under test. 如果做对了,这将照顾到身份验证所需的绑定,从而有效地测试WCF配置。

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

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