Asp.net核心Web应用程序中的Asmx Web服务基本身份验证

时间:2017-03-20 10:02:38

标签: web-services wcf asp.net-core asmx

我创建了一个Asmx Web服务并将其托管在IIS中,在MVC中,我可以从下面的代码中调用它:

        BasicWebService.WebService1 client = new BasicWebService.WebService1();
        client.Credentials = new System.Net.NetworkCredential("user", "pwd","domain");
        string result = client.HelloWorld();

但是,我未能在Asp.net Core下标记它。 这是我试过的代码。

 ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient(ServiceReference1.WebService1SoapClient.EndpointConfiguration.WebService1Soap);
        client.ClientCredentials.UserName.UserName = "xx";
        client.ClientCredentials.UserName.Password = "xx";   

        //string USER = "xx";
        //string PASSWORD = "xx";
        //string Domain = "xx";
        //NetworkCredential netCredential = new NetworkCredential(USER, PASSWORD,Domain);
        ////client.Credentials = new System.Net.NetworkCredential("xx", "xx", "xx");
        //client.ClientCredentials.Windows.ClientCredential = netCredential;// netCredential.GetCredential(new Uri("http://localhost/WCFBasicSecurity/WebService1.asmx"), "Basic");
        ServiceReference1.HelloWorldResponse result =client.HelloWorldAsync().Result;

1 个答案:

答案 0 :(得分:0)

从GitHub获得解决方案,感谢hongdai的建议。使用以下命令修改生成的reference.cs:

public WebService1SoapClient(EndpointConfiguration endpointConfiguration) : 
        base(WebService1SoapClient.GetBindingForEndpoint(endpointConfiguration), WebService1SoapClient.GetEndpointAddress(endpointConfiguration))
{
    this.Endpoint.Name = endpointConfiguration.ToString();
    this.ChannelFactory.Credentials.UserName.UserName = "xx\xx";
    this.ChannelFactory.Credentials.UserName.Password = "xxxx";
    ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
    if ((endpointConfiguration == EndpointConfiguration.WebService1Soap))
    {

        //Transport Security
        System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding(System.ServiceModel.BasicHttpSecurityMode.Transport);
        result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Basic;            
        result.MaxBufferSize = int.MaxValue;
        result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
        result.MaxReceivedMessageSize = int.MaxValue;
        result.AllowCookies = true;
        return result;
    }
    if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12))
    {
        System.ServiceModel.Channels.CustomBinding result = new System.ServiceModel.Channels.CustomBinding();
        System.ServiceModel.Channels.TextMessageEncodingBindingElement textBindingElement = new System.ServiceModel.Channels.TextMessageEncodingBindingElement();
        textBindingElement.MessageVersion = System.ServiceModel.Channels.MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap12, System.ServiceModel.Channels.AddressingVersion.None);
        result.Elements.Add(textBindingElement);
        System.ServiceModel.Channels.HttpTransportBindingElement httpBindingElement = new System.ServiceModel.Channels.HttpTransportBindingElement();
        httpBindingElement.AllowCookies = true;
        httpBindingElement.MaxBufferSize = int.MaxValue;
        httpBindingElement.MaxReceivedMessageSize = int.MaxValue;
        result.Elements.Add(httpBindingElement);
        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.WebService1Soap))
        {
            return new System.ServiceModel.EndpointAddress("https://localhost/WCFBasicSecurity/WebService1.asmx");
            //return new System.ServiceModel.EndpointAddress("http://localhost/WCFBasicSecurity/WebService1.asmx");
        }
        if ((endpointConfiguration == EndpointConfiguration.WebService1Soap12))
        {
            return new System.ServiceModel.EndpointAddress("http://localhost/WCFBasicSecurity/WebService1.asmx");
        }
        throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
    }

参考:https://github.com/dotnet/wcf/issues/1812