[英]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 也许这可以帮助您LINK , LINK
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.