问题
I'm newer to web services, but I have a web service that I'm consuming/wrapping in Visual Studio C# that ends in Service.asmx
where I did Add Service Reference
and it pulled in all of the elements that were showing in Service.asmx?wsdl
.
It has objects for Username
and Password
that worked fine, but now the company who created this web service is changing to token authentication where I get a GUID token and pass null to these elements.
Well shouldn't the web service show a new element where I pass the token?
I'm being told that the token gets passed in the header with field name Auth-Token
. So is this something obvious I should do or does the WSDL need updated?
回答1:
So I figured out more info which may help someone else. I added the service as a "Service Reference", which is the more advanced WCF, but it's an asmx
reference which is a "Web Reference".
When you add a service reference, if you click advanced options, there is the older "Web Reference" option which uses .Net 2.0 style.
From there, you just choose your proxy class and override the System.Net.WebRequest GetWebRequest
method like this:
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
request.Headers.Add("Auth-Token", this.authToken);
return request;
}
来源:https://stackoverflow.com/questions/33532503/how-to-add-token-authentication-to-web-services-for-auth-token