Search code examples
c#wcf.net-8.0.net-4.8

VS 2022 Add Service Reference not generating ClientCredentials for .Net Framework


I have to add a service reference to create a client proxy for a .Net Framework service and a .Net 8 service. The .Net 8 Add Service reference generates the client proxy correctly and executes without issue. In a separate project / Framework based service, the generated proxy doesn't include ClientCredentials.

For the following policy from the wsdl:

  <wsp:Policy wsu:Id="WSHttpBinding_IQuery_2012_04_policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
          <wsp:Policy>
            <sp:TransportToken>
              <wsp:Policy>
                <sp:HttpsToken RequireClientCertificate="false" />
              </wsp:Policy>
            </sp:TransportToken>
            <sp:AlgorithmSuite>
              <wsp:Policy>
                <sp:Basic256 />
              </wsp:Policy>
            </sp:AlgorithmSuite>
            <sp:Layout>
              <wsp:Policy>
                <sp:Strict />
              </wsp:Policy>
            </sp:Layout>
            <sp:IncludeTimestamp />
          </wsp:Policy>
        </sp:TransportBinding>
        <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
          <wsp:Policy>
            <sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
              <wsp:Policy>
                <sp:WssUsernameToken10 />
              </wsp:Policy>
            </sp:UsernameToken>
          </wsp:Policy>
        </sp:SignedSupportingTokens>
        <sp:Wss11 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
          <wsp:Policy />
        </sp:Wss11>
        <sp:Trust10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
          <wsp:Policy>
            <sp:MustSupportIssuedTokens />
            <sp:RequireClientEntropy />
            <sp:RequireServerEntropy />
          </wsp:Policy>
        </sp:Trust10>
        <wsaw:UsingAddressing />
      </wsp:All>
    </wsp:ExactlyOne>
  </wsp:Policy>

It does generate the correct binding / client info:

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IQuery_2012_04">
                <security mode="TransportWithMessageCredential">
                    <transport clientCredentialType="None" />
                    <message clientCredentialType="UserName" establishSecurityContext="false" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://somehost/ptsqamt/Maintain/Services/Data/2012/04/Query.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IQuery_2012_04"
            contract="PTSQueryService.IQuery_2012_04" name="WSHttpBinding_IQuery_2012_04" />
    </client>
</system.serviceModel>

The generated client proxy doesn't create the ClientCredentials, so I can't add the user / password.

Generated client snippet for the .Net Framework reference:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Query_2012_04Client : System.ServiceModel.ClientBase<WCFTest.PTSQueryService.IQuery_2012_04>, WCFTest.PTSQueryService.IQuery_2012_04 {
    
    public Query_2012_04Client() {
    }
    
    public Query_2012_04Client(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }
    
    public Query_2012_04Client(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }
    
    public Query_2012_04Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }
    
    public Query_2012_04Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }
    
    public WCFTest.PTSQueryService.QueryParameter[] GetQueryParameters(int qtype, string qname) {
        return base.Channel.GetQueryParameters(qtype, qname);
    }
    
    public System.Threading.Tasks.Task<WCFTest.PTSQueryService.QueryParameter[]> GetQueryParametersAsync(int qtype, string qname) {
        return base.Channel.GetQueryParametersAsync(qtype, qname);
    }
    
    public string GetQueryData(int qtype, string qname, WCFTest.PTSQueryService.QueryArgument[] args, int page, int pageSize) {
        return base.Channel.GetQueryData(qtype, qname, args, page, pageSize);
    }
    
    public System.Threading.Tasks.Task<string> GetQueryDataAsync(int qtype, string qname, WCFTest.PTSQueryService.QueryArgument[] args, int page, int pageSize) {
        return base.Channel.GetQueryDataAsync(qtype, qname, args, page, pageSize);
    }
}

For Reference, here's what the .Net 8 project generated:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.2.0-preview1.23462.5")]
public partial class Query_2012_04Client : System.ServiceModel.ClientBase<PTSQueryService.IQuery_2012_04>, PTSQueryService.IQuery_2012_04
{
    
    /// <summary>
    /// Implement this partial method to configure the service endpoint.
    /// </summary>
    /// <param name="serviceEndpoint">The endpoint to configure</param>
    /// <param name="clientCredentials">The client credentials</param>
    static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
    
    public Query_2012_04Client() : 
            base(Query_2012_04Client.GetDefaultBinding(), Query_2012_04Client.GetDefaultEndpointAddress())
    {
        this.Endpoint.Name = EndpointConfiguration.WSHttpBinding_IQuery_2012_04.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public Query_2012_04Client(EndpointConfiguration endpointConfiguration) : 
            base(Query_2012_04Client.GetBindingForEndpoint(endpointConfiguration), Query_2012_04Client.GetEndpointAddress(endpointConfiguration))
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public Query_2012_04Client(EndpointConfiguration endpointConfiguration, string remoteAddress) : 
            base(Query_2012_04Client.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public Query_2012_04Client(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(Query_2012_04Client.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
    {
        this.Endpoint.Name = endpointConfiguration.ToString();
        ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
    }
    
    public Query_2012_04Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress)
    {
    }
    
    public System.Threading.Tasks.Task<PTSQueryService.QueryParameter[]> GetQueryParametersAsync(int qtype, string qname)
    {
        return base.Channel.GetQueryParametersAsync(qtype, qname);
    }
    
    public System.Threading.Tasks.Task<string> GetQueryDataAsync(int qtype, string qname, PTSQueryService.QueryArgument[] args, int page, int pageSize)
    {
        return base.Channel.GetQueryDataAsync(qtype, qname, args, page, pageSize);
    }
    
    public virtual System.Threading.Tasks.Task OpenAsync()
    {
        return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
    }
    
    private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
    {
        if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_IQuery_2012_04))
        {
            System.ServiceModel.WSHttpBinding result = new System.ServiceModel.WSHttpBinding();
            result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            result.MaxReceivedMessageSize = int.MaxValue;
            result.AllowCookies = true;
            result.Security.Mode = System.ServiceModel.SecurityMode.TransportWithMessageCredential;
            result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
            result.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName;
            result.Security.Message.EstablishSecurityContext = false;
            return result;
        }
        throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
    }
    
    private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
    {
        if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_IQuery_2012_04))
        {
            return new System.ServiceModel.EndpointAddress("https://somehost/ptsqamt/Maintain/Services/Data/2012/04/Query.s" +
                    "vc");
        }
        throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
    }
    
    private static System.ServiceModel.Channels.Binding GetDefaultBinding()
    {
        return Query_2012_04Client.GetBindingForEndpoint(EndpointConfiguration.WSHttpBinding_IQuery_2012_04);
    }
    
    private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
    {
        return Query_2012_04Client.GetEndpointAddress(EndpointConfiguration.WSHttpBinding_IQuery_2012_04);
    }
    
    public enum EndpointConfiguration
    {
        
        WSHttpBinding_IQuery_2012_04,
    }
}

Thanks for any help you can offer.


Solution

  • ClientCredentials exists in System.ServiceModel.ClientBase < TChannel >.

    enter image description here

    You can use it like this:

    Add a validation class to the server: public class CustomUserNameValidator : UserNamePasswordValidator {

       public override void Validate(string userName, string password)
        {
            if (null == userName || null == password)
            {
                throw new ArgumentNullException();
            }
            if (userName != "admin" && password != "wcf.admin") 
            {
                throw new System.IdentityModel.Tokens.SecurityTokenException("Unknown Username or Password");
            }
    
        }
    }
    

    Use this code in the client to add a username and password:

    using (var proxy = new ServiceReference1.Service1Client())
    {
        proxy.ClientCredentials.UserName.UserName = "admin";
        proxy.ClientCredentials.UserName.Password = "wcf.admin";
        string result = proxy.GetData(1);
        Console.WriteLine(result);
        var compositeObj = proxy.GetDataUsingDataContract(new CompositeType() { BoolValue = true, StringValue = "test" });
        Console.WriteLine(SerializerToJson(compositeObj));
    }
    Console.ReadKey();
    

    If there is a discrepancy, an error will occur

    enter image description here