3

I'm creating an application with phonegap on windows phone 7, that need an authentification on a server. When we log with the correct username and password, we got a 200 status. And everything works. But if we make a mistake on the login or the password, the server send us back a 401 error. This freeze my application. I need to kill it and restart it. So not very practice.

I check the response with fiddler on my computer and the phone receive the 401 error, and I manage this error on my code. this works on my other platform (android and ios).

So I would like to know how I can manage this error. Maybe I can change a cs file on the windows phone project to handle this error.

any help is welcome

Here is the code

$.support.cors = true;
    $.ajax({
        type: "POST",
        dataType: "HTTP/1.1",
        url: 'https://xxx.xxx-xxx.com/issue/wrap',
        data: data,
        cache: 'false',
        async: false,
        error: function (data) {
            console.log(data);
            console.log("error");
            //navigator.notification.alert(data);
        },
        complete: saveToken
    });

thanks

dpfauwadel
  • 3,866
  • 3
  • 23
  • 40

3 Answers3

2

I've encountered the same problem and the only solution I founded is to implement a Phonegap plugin.

Here is the C# code I use :

namespace WPCordovaClassLib.Cordova.Commands
{
    [DataContract]
    public class PhonegapWindowsPhonePostObject
    {
        [DataMember(IsRequired = false, Name = "yourParamName1")]
        public string yourParam1;

        [DataMember(IsRequired = false, Name = "yourParamName2")]
        public string yourParam2;
    }

    public class PhonegapWindowsPhonePost: BaseCommand
    {
        public void post(string options)
        {
            PhonegapWindowsPhonePostObject pwppo = JSON.JsonHelper.Deserialize<PhonegapWindowsPhonePostObject>(options);

            try
            {
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    var data = "YOUR DATA STRING HERE"; //use the pwppo object to retrieve your parameters
                    var url = "URL OF THE SERVICE HERE";


                    WebClient wc = new SharpGIS.GZipWebClient();
                    var URI = new Uri(url);
                    wc.Encoding = Encoding.UTF8;
                    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc__UploadStringCompleted);
                    wc.UploadStringAsync(URI, "POST", data);
                });

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        void wc__UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null)
                    this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, e.Result));
                else
                    this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error 401"));
            }
            catch (Exception ex)
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ex.Message));
                // no http status code available
            }
        }
    }
}

And here is the Javascript code to call the pluging from your app :

function loginToWebService(yourParam1, yourParam2) {

    var options = { "yourParamName1": yourParam1, "yourParamName2": yourParam2};
    cordova.exec(success, error,"PhonegapWindowsPhonePost","post", options);
}

I hope it will help you.

Note : the Phonegap 2.8 version does not solve the problem contrary to what said the release note...

Bye !

Antoine
  • 548
  • 4
  • 13
  • Hope this problem will be solved by Phonegap or Microsoft if its due to IE – Antoine Jun 11 '13 at 14:14
  • I opened an issue on the cordova issue tracker as I had a similar / the same problem:https://issues.apache.org/jira/browse/CB-4000?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13693241#comment-13693241 – Thomas Kremmel Jun 26 '13 at 07:35
  • I had troubles even with Cordova 3.1 - I posted my solution here http://stackoverflow.com/a/20306485/1994642 . It is a plugin aswell. – stiller_leser Nov 30 '13 at 22:45
0

Sup, how are you doing the authentication?

if ur using XMLHttpRequest() like : var request = new XMLHttpRequest();

try to check what happens with somth like: console.log("status: " + request.status);

If it seems to work, u can use:

if (request.status == 401) {
    // blabla
return;
}
Enoque Duarte
  • 689
  • 4
  • 22
  • I already handle the error like this. But the problem is that the application just freeze when the 401 is returned. We can't handle the response in the javascript code. – dpfauwadel Jun 07 '13 at 15:46
  • well, sry then, no idea, chill out and make some popcorns – Enoque Duarte Jun 07 '13 at 15:49
  • have you tried to wrap the request on a try catch ? -tryception :D – Enoque Duarte Jun 07 '13 at 15:52
  • why dont u make the server-side return like a string "denied" instead of error? – Enoque Duarte Jun 07 '13 at 16:14
  • I'm trying to find the documentation, but basically PhoneGap does not catch 401 status responses. For this, I implemented a 10 second timeout on my login attempts, then just "assume" that if it times-out, it's a bad username/password. Really hope Cordova gets this fixed! – ganders Aug 25 '14 at 14:19
0

I've had this problem for the last couple months and finally figured it out.

First of all, the temporary workaround that we used was to implement a timeout period on the request. That worked, but the response was always a 404. We didn't know if it actually failed authentication, or if our services were down. Not an acceptable workaround for our system.

Finally, after MUCH googling, I found the actual problem.

When a 401 gets returned, (which is a Basic Authentication, authentication failure), it adds a header to the response, called "WWW-Authenticate". This tells the browser to prompt the user to try and login. iOS webview doesn't know what to do with that, so that's why the application hangs.

From your service, all you need to do is remove the WWW-Authenticate header, and you should start to see the 401's in your error function.

ganders
  • 7,285
  • 17
  • 66
  • 114