简体   繁体   English

如何为“ Auth-Token”的Web服务添加令牌认证?

[英]How to add token authentication to web services for “Auth-Token”?

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 . 我是Web服务的新手,但是我有一个在Visual Studio C#中使用/包装的Web服务,该服务以Service.asmx结尾,在其中我做了“ Add Service Reference ,并提取了Service.asmx?wsdl中显示的所有元素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. 它具有用于UsernamePassword对象,这些对象可以正常工作,但是现在创建此Web服务的公司正在更改为令牌身份验证,在这里我获得了GUID令牌并将null传递给这些元素。

Well shouldn't the web service show a new element where I pass the token? 那么,Web服务是否应该在传递令牌的地方显示一个新元素?

I'm being told that the token gets passed in the header with field name Auth-Token . 有人告诉我令牌在字段名称为Auth-Token的标头中传递。 So is this something obvious I should do or does the WSDL need updated? 那么这是显而易见的事吗,还是需要更新WSDL?

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". 我将服务添加为“服务参考”,这是更高级的WCF,但它是asmx参考,即“ 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. 添加服务引用时,如果单击高级选项,则存在使用.Net 2.0样式的较旧的“ Web参考”选项。

From there, you just choose your proxy class and override the System.Net.WebRequest GetWebRequest method like this: 从那里,您只需选择代理类并重写System.Net.WebRequest GetWebRequest方法,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM