如何为"Auth-Token"向Web服务添加令牌认证? [英] How to add token authentication to web services for "Auth-Token"?
问题描述
我是Web服务的新手,但是我有一个要在Visual Studio C#中使用/包装的Web服务,该服务以Service.asmx
结尾,在其中我做了Add Service Reference
,并且拉入了所有在Service.asmx?wsdl
中显示.
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
.
它具有用于Username
和Password
的对象,它们可以正常工作,但是现在创建此Web服务的公司正在更改为令牌身份验证,在那里我获得了GUID令牌并将null传递给这些元素.
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.
Web服务是否应该在传递令牌的地方显示一个新元素?
Well shouldn't the web service show a new element where I pass the token?
有人告诉我令牌在字段名称为Auth-Token
的标头中传递.那么这是显而易见的事吗,还是需要更新WSDL?
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?
推荐答案
所以我想出了更多信息,可能会对其他人有所帮助.我将服务添加为服务参考",这是更高级的WCF,但它是asmx
参考,它是"Web参考".
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".
添加服务引用时,如果单击高级选项,则存在使用.Net 2.0样式的较旧的"Web引用"选项.
When you add a service reference, if you click advanced options, there is the older "Web Reference" option which uses .Net 2.0 style.
从那里,您只需选择代理类并重写System.Net.WebRequest GetWebRequest
方法,如下所示:
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;
}
这篇关于如何为"Auth-Token"向Web服务添加令牌认证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!