Skip to main content
Participating Frequently
June 23, 2007
Question

Basic authentication when calling a web service

  • June 23, 2007
  • 9 replies
  • 14508 views
I am attempting to call a web service using ActionScript. The web service provider requires that I use HTTP Basic Authentication to communicate my SOAP requests. I cannot seem to get this done in ActionScript. If I instantiate a WebService object and call its SetCredentials method, I get an error "Authentication not supported on DirectHTTPChannel (no proxy)". I have the WebService object's useProxy property set to true. HELP!
    This topic has been closed for replies.

    9 replies

    Participant
    April 1, 2009
    I am trying to access a .net webservice hosted on an IIS server protected by basic authentication. I was trying to do as gkohen has done to import the wsdl in flex builder. But when I enter the wsdl and press next, in the import wsdl wizard, it is bringing up an authentication form asking for username and pasword. When I enter the username and password and press ok, it is not working. It is bringing up the the authentication form again asking for username and password. Due to this, the Flex Builder is not able to auto-generate the code for the web services. It is showing an error "unable to load wsdl". The same wsdl is working with WebService object (without using the import wsdl feature).
    It is still bringing up the authentication form but when I enter the credentials manually, it is returning a response. Can anyone suggest a way to get the import wsdl feature working for a webservice protected by basic authentication?
    Participant
    March 5, 2008
    Another possible hack is to pass the credentials as a GET parameter, and have your endpoint web server convert the parameter into the Authentication HTTP header.

    In flex-land, modify the URL of the webservice as follows:

    public function
    setCredentials(username;String, password:String):void
    {
    var base64Encoder:Base64Encoder = new Base64Encoder();
    base64Encoder.encodeUTFBytes(username+':'+password+"\n");
    endpointURI = endpointURI + '?' + base64Encoder.toString();
    }

    In apache-land, add the following directives to convert the GET parameter to a header:

    SetEnvIfNoCase Authorization ^.*$ NO_Authorization
    RequestHeader add Authorization "Basic ${QUERY_STRING}e" env=NO_Authorization

    (delete the newline before env=... or Apache will complain)
    Participant
    March 11, 2008
    Have done a lot of research of this last week. Here are my findings: there is no way to programmatically invoke a web service when its wsdl is protected by http basic authentication in Flex (2, 3) and this CAN be done for http services. If looking at following code to access a web service and a http service:
    var ws1:WebService = new WebService();
    ws1.httpHeaders = {Authorization:"Basic a2F0emo6a2F0ejAxNmoo="};
    ws1.wsdl = " http://hostname/Temp-WS-context-root/WSWSSoapHttpPort?wsdl";
    ws1.loadWSDL();
    ws1.superTest.send();
    ...
    var hs1:HttpService= new HttpService();
    hs1.httpHeaders = {Authorization:"Basic a2F0emo6a2F0ejAxNmoo="};
    hs1.method = "GET";
    hs1.url = " http://hostname/Temp-WS-context-root/test.jsp";
    hs1.send();

    Both have the header for basic authentication, the second succeeds and the first one pops up the login dialog. After went through the flex code, I found flex invoking a web service by 2 http service calls: one for wsdl and one for the endpoint. The issue is the one loading the wsdl does not take the header for basic authentication! Take look the initializeService method of the XMLLoader class (superclass of wsdlloader) where the new http service for load wsdl is created. Also to verify this, you may unprotect the wsdl file. I am surprised that adobe concluded this bug (https://bugs.adobe.com/jira/browse/SDK-12198) as "cannot fix" due to a flash player bug. I have to say my player sends http get request with headers just fine. I believe this is still an overlooked flex code issue.


    Participant
    March 12, 2008
    Some corrections. It is true that flash player 9 does not allow setting security headers in a httpservice. But Air does. So in general flex should pass the security header to the wsdl loading httpservice.
    Participant
    January 24, 2008
    I use something similar to nijlz above, but we're in control of our own servers. Our server is setup so that the http handler creates and manages the user sessions, and once the user has a valid session, they can access the WebServices behind that. And to use from Flex, we using an HTTPService call first, and then we make calls to WebServices.

    A lot of what everyone is talking about with not accessing WSDL files seems more to do with the Web Service Provider's limiting factors, than Flex per say. I'm finding this a lot with people who only consume and produce Web Services in a .NET only environment. Web Services in .NET work really well with itself, but when it comes to accessing from a variety of third-party clients...not so much, mostly because the people who created them live in a Microsoft vacuum. This isn't just a Flex thing, but I see it when trying to consume various Web Services from various technologies. It's probably why people are getting frustrated and moving to a simpler RESTful style request/response. (Sorry to go a little off-topic.)
    Participant
    July 8, 2007
    Guys,

    the easiest way to solve this problem is to use "inline" basic authentication this way:

    <mx:Webservice wsdl=" http://{ws_username}:{ws_password}@your_ws_url" />

    It works 100%, but you have to be be aware of two things:
    1) you need your ws_username and ws_password must be URLEncode'd, so for example "@" character of email address will turn into %40 and so...

    2) if someone will want to sniff for your password and user name, he can do it very easy by looking into "Authentication" HTTP header same way as u URL and extract your credentials, all you have to do is Base64 decode.

    Hope this helps. if you have more questions, drop me a line or gtalk to me on info [at] skitsanos [dot] com

    Cheers,
    Evi Skitsanos.
    August 16, 2007
    Hi Evi,

    that workaround does not help, if the WSDL is loaded from another location than the one that is called for the WebService.

    I currently have a scenario where i have the WSDLs deployed together with the application because they are not provided at the URL where the WebService itself is located. This might be a quite special case but even these special cases happen to exist.

    Regards, Roland
    Participant
    August 16, 2007
    I am in the same boat. I am calling a WCF service from FLEX and I need to supply userID and password to authenticate the user. Has anyone figured out a solution to this problem?
    Participating Frequently
    July 1, 2007
    Bump -- Adobe, can you help?
    Inspiring
    June 27, 2007
    Participating Frequently
    June 27, 2007
    Unfortunately, this post discusses HTTP Basic Authentication for the HTTPService class, which does not process SOAP like the WebService class does. In fact, one of the replies in the thread you pointed me at is another poor guy, with the exact same question, who is also being ignored by Adobe. There are many users with this problem -- Adobe should help us out, here!

    (From the thread suggested by the previous poster:)

    quote:

    Hi Abdul,
    I am looking at a similar problem with slightly different twist. It is best described by a question posted here http://192.150.14.120/cfusion/webforums/forum/messageview.cfm?catid=582&threadid=1160651&enterthread=y

    "I would like a standalone Flex 2.0 SWF to be able to use a set of 3rd party webservices protected by basic authentication. Is it possible? If yes, could someone put a simple step-by-step example up? I just can't see anyway to do this from the livedocs entry."

    The stuff livedocs and other places seem to imply a Flex or CF server in the back-end.

    What you have could be used to address this but I am not able to get it to work with the WebService object. I am using your code to do basic HTTP authentication then invoke calls using the WebService object. Though the authentication suceeds I still get prompted for user/password when I make the WebService call.

    June 27, 2007
    One other way is to proxy the webservice call through a server-side platform.

    I never call any data service directly from Flex.

    Tracy
    Participating Frequently
    June 27, 2007
    Hi Tracy, thanks for the input. I am working with a third party and the client application needs to call their web services. Yes, I could (and have) proxied the call through my own server (I take the POST in my own server, and relay it on to the third party services -- C# does not have a problem calling a web service with basic authentication). But the third party that supplies this web service operates a huge server farm that can literally service hundreds of thousands of transactions per minute. I would have to build my own farm of proxy servers just to work around a shortcoming in Flex (and the apparent unwillingness of Adobe to even acknowledge it). My farm would have to be capable of matching the level of service of the third party, and this would would be very expensive to build and operate (even though all it would be doing is adding an authentication header and then relaying requests on to the third party web service)

    You state that you always call through a proxy server instead of calling a web service directly from Flex. Is there some other advantage to this approach that I am missing?

    Thanks again!

    Greg
    June 26, 2007
    I searched quite long for a solution to this too.

    But it seems there is none.

    I hope this will no longer be an issue in Flex 3 because the problem is common and quite trivial and I don't see a reason why this should not be possible with webservices.

    Kind regards, Roland
    Participating Frequently
    June 26, 2007
    Adobe, help! It seems that others have tried in vain to solve this problem -- it's a very basic problem: How do I call a Web Service using HTTP basic authentication? If I had the source to the WebService class I could add the HTTP authentication header myself inside there... but there must be some other way!

    HELP!
    Participating Frequently
    June 23, 2007
    Adobe, please help!

    I have seen this question posed five other times in this forum, but I cannot find an answer anywhere. The problem itself is simple: I want to call a SOAP-based Web Service that is protected on its server via HTTP Basic Authentication.

    I would like to use ActionScript like the following, but I cannot ever get it to work. Like the others who have posted this same question, I have tried all permutations of setCredentials, setRemoteCredentials, and useProxy, but I cannot figure out how to specify the username/password combo that is required by the server when it performs its Basic HTTP Authentication.

    This seems to be a common problem without a solution -- can anyone help, please?

    Thanks in advance

    -- Greg

    var ws:WebService;

    function main () : void
    {
    ws = new WebService ();

    ws.addEventListener("load", wsdlLoadHandler);
    ws.loadWSDL("myWsdlFile.wsdl");
    }

    function wsdlLoadHandler (event:LoadEvent) : void
    {
    ws.addEventListener (ResultEvent.RESULT, wsComplete);
    ws.addEventListener (FaultEvent.FAULT, wsFault);

    ws.someFunction.addEventListener (ResultEvent.RESULT, wsComplete);
    ws.someFunction.addEventListener (FaultEvent.FAULT, wsFault);

    ws.endpointURI = "url_to_some_web_service_that_requires_basic_HTTP_authentication";

    ws.someFunction (3.14159);
    }

    private function wsComplete (event:ResultEvent) : void
    {
    var xyz:int = 5;
    }

    private function wsFault (event:FaultEvent) : void
    {
    var xyz:int = 5;
    }