gpt4 book ai didi

c# - 远程服务器返回错误 : (401) Unauthorized. wcf httpbinding basic

转载 作者:行者123 更新时间:2023-11-30 21:38:39 24 4
gpt4 key购买 nike

我创建了一个简单的 wcf 服务 [ServiceContract]

 public interface IService1
{

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

string GetData(string data);

}

使用此自定义身份验证:

   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();
}
}

还有这个网络配置:

 <bindings > 

<webHttpBinding>

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

</webHttpBinding>
</bindings>

客户端代码:

 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);

但是当我运行客户端代码时,我得到这个错误:

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

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

最佳答案

试试这个授权选项:

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

关于c# - 远程服务器返回错误 : (401) Unauthorized. wcf httpbinding basic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com