【问题标题】:Asmx Web Service Basic Authentication in Asp.net Core Web ApplicationAsp.net Core Web 应用程序中的 Asmx Web 服务基本身份验证
【发布时间】:2017-03-20 10:02:38
【问题描述】:

我创建了一个 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;

【问题讨论】:

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


    【解决方案1】:

    从 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      相关资源
      最近更新 更多