Search code examples
c#wcfbasic-authenticationazure-relay

How do I send basic authentication credentials for a prem wcf https service over azure relay C#?


I have a WebAapp on Azure that sends a request to Azure Relay. It should transfer to a listener on premises WCF HTTPS service hosted on IIS that requires basic authentication. How do I send authorization basic header for the onprem WCF service over the Azure Relay . How do I send ? example,

"Authorization": "Basic 239837987XYC"

I have used channel factory,

        var  ChannelFactory<Overview.MyChannel> cf;
        var relayNamespace ="myrelaynamespace";
        var relayListener = "myrelaylistener";
        var endPointAddress = new EndpointAddress(ServiceBusEnvironment.CreateServiceUri("https", relayNamespace, relayListener));
        cf = new ChannelFactory<Overview.ItServiceManagementAOChannel>(binding, endPointAddress);
        ClientCredentials loginCredentials = new ClientCredentials();
        loginCredentials.UserName.UserName = "onpremWCFusername";
        loginCredentials.UserName.Password = "onpremWCFpassword";

        cf.Endpoint.Behaviors.Add(new TransportClientEndpointBehavior
        {
            TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(ConfigurationManager.AppSettings.Get("WcfRelayKeyName"), ConfigurationManager.AppSettings.Get("WcfRelayKey"))
        });
        cf.Endpoint.Behaviors.Add(loginCredentials); 

I get the Error: The value could not be added to the collection, as the collection already contains an item of the same type: 'System.ServiceModel.Description.ClientCredentials'. This collection only supports one instance of each type. Parameter name: item

   using (var ch = cf.CreateChannel())
        {
           try
            {
               var resp = ch.CreateTaskAsync(req).Result;
            }
         }

Solution

  • Try to specify the windows credential as client credential.

    factory.Credentials.Windows.ClientCredential.UserName = "administrator";
    factory.Credentials.Windows.ClientCredential.Password = "123456";
    IService sv = factory.CreateChannel();
    

    Feel free to let me know if the problem still exists.