I create a simple wcf service [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 :
<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.