简体   繁体   English

远程服务器返回错误:(401)未经授权。 wcf httpbinding基本

[英]The remote server returned an error: (401) Unauthorized. wcf httpbinding basic

I create a simple wcf service [ServiceContract] 我创建一个简单的wcf服务[ServiceContract]

 public interface IService1
    {

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/Data/{data}")]

        string GetData(string data);

    }

With this custom auth validation : 使用此自定义身份验证:

   public class userpass : UserNamePasswordValidator
        {
            public override void Validate(string userName, string password)
            {
                if (string.Equals(userName, "1", StringComparison.OrdinalIgnoreCase)
                    && password == "1")
                    return;
                throw new SecurityTokenValidationException();
            }
        }

And this webconfig : 而这个webconfig:

 <bindings > 

      <webHttpBinding>

        <binding>
          <security mode="Transport">
            <transport clientCredentialType="Basic"/>
          </security>
        </binding>

      </webHttpBinding>
    </bindings>

And

And the client code : 和客户端代码:

 Uri reqUri = new Uri("https://union-pc58.union.com/Service1.svc/data/asdsad");



            WebRequest req = WebRequest.Create(reqUri);

            req.PreAuthenticate = true;

            NetworkCredential credential = new NetworkCredential("1", "1");

            req.Credentials = credential;

            WebResponse resp = req.GetResponse();


            DataContractSerializer data = new DataContractSerializer(typeof(string));
            var res = data.ReadObject(resp.GetResponseStream());

            Console.WriteLine(res);

But when i run the client code i get this error : 但是当我运行客户端代码时,出现此错误:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (401) Unauthorized.

Try this authorization option instead: 请尝试以下授权选项:

string credentials = "1:1";
req.Headers.Add(HttpRequestHeader.Authorization, "Basic "+ Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));

Did you install a certification? 您安装了认证吗? Maybe this could help you LINK , LINK 也许这可以帮助您LINKLINK

I got the same problems, because my web-service couldnt find the certification for https. 我遇到了同样的问题,因为我的网络服务无法找到https的证书。 Here ist a peace of my config: 在这里,我的配置很安全:

<system.serviceModel>
    <services>
      <service name="MyApp.Service.ServiceControl.WCF.WcfService">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:9999/MyApp/Services" />
          </baseAddresses>
        </host>
        <endpoint address="https://localhost:9999/MyApp/Services" binding="basicHttpsBinding" contract="MyApp.Service.ServiceControl.WCF.IWcfService" bindingConfiguration="TransportSecurity">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpsBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </basicHttpsBinding>
    </bindings>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

暂无
暂无

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

相关问题 远程服务器返回错误:(401) Unauthorized。 Twitter oAuth - The remote server returned an error: (401) Unauthorized. Twitter oAuth C#JIRA工作日志更新错误“远程服务器返回错误:(401)未经授权。” - C# JIRA work-log update error “The remote server returned an error: (401) Unauthorized.” System.Net.WebException:远程服务器返回错误:(401)未经授权。 在System.Net.HttpWebRequest.GetResponse() - System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() SharePoint 仅应用程序身份验证抛出“远程服务器返回错误:(401)未经授权。” - SharePoint App-Only Authentication throwing “The remote server returned an error: (401) Unauthorized.” 远程服务器返回错误:(401) 未经授权。 在 Visa 开发支付网关 - The remote server returned an error: (401) Unauthorized. In Visa develope payment Gateway 为什么我收到“远程服务器返回错误:(401) 未经授权”。 从这个代码片段? - Why am I getting 'The remote server returned an error: (401) Unauthorized.' from this code snippet? 出现错误:远程服务器返回错误:(401)未经授权。 ,同时将我的网站评论发布到Twitter。 使用C# - Getting error : The remote server returned an error: (401) Unauthorized. , while posting my website comments to twitter. Using C# EWS:“远程服务器返回错误(401)未经授权” - EWS: “The remote server returned error (401) Unauthorized” 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM