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

五迷三道 提交于 2020-07-17 08:54:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!