Azure Mobile Service error: The request could not be completed. (Unauthorized)

2.6k Views Asked by At

I'm writing an Windows Phone 8.1 mobile application and data source for the app is Azure Mobile Service.

Since last two days my login function stopped working. Now it is throwing The request could not be completed. (Unauthorized) error.

I'm surprised that without making any changes how the login service stopped? Other services are still working.

Fill error stack trace is:

The request could not be completed. (Unauthorized)

{Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The request could not be completed. (Unauthorized) at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.d__1d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<RequestAsync>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.WindowsAzure.MobileServices.MobileServiceClient.d__b.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.WindowsAzure.MobileServices.MobileServiceClient.<InvokeApiAsync>d__02.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at MYAPP.SignIn.d__1.MoveNext()}

My code to call login service is is:

var client = new MobileServiceClient("https://<My-Azure-Service>.azure-mobile.net/", "***KEY***");

var myLogin = new LoginRequest
                {
                    UserName = "username",
                    Password = "password",
                    DeviceToken = deviceID,
                    OSVersion = "WP 8",
                    AppVersion = "The major & minor version",
                    DeviceModel = "WP",
                    DeviceTypeID = 3
                };
                var loginResult = await client.InvokeApiAsync<LoginRequest, LoginResponse>("user/loguserin", myLogin, HttpMethod.Post, null);

Please help me to solve this issue.

1

There are 1 best solutions below

0
On

The Unauthorized error is coming from the server, and that could be due to a) authorization issues with the API, or b) issues in the code of the API.

The latter is something you will have to work with your client on. Because the code is performing login, there are many cases that might result in a 401 Unauthorized. This is not the standard Mobile Services login functionality, and your client is in control of the code executing and what errors are returned at this point.

Speaking to the authorization issues on the API, Mobile Services allows the API developer to restrict access to one of four levels:

  • Everyone (or "Anonymous") - fully public API that anyone can access (recommended for login endpoints
  • Application Key - only available to those providing the application key. The application key is provided by the server and is the value labeled ***KEY*** in the code from the original post. This key is attached to all requests to the Mobile Service
  • Master Key - only available to those providing the master key. The master key is provided by the server and should never be stored in the device code.
  • Authenticated users - available only to those providing a valid Mobile Services token. This token is normally obtained by having the user sign in through one of the supported 3rd party identity providers, but in this case, the token is being generated by your client's code based on some username and password they maintain.

If the API is set to one of these, and the call from the device does not meet the requirements, a 401 will be returned. You should check with your client to find out what authorization level they have set the login API to.

It's hard to say why this would work for iPhone/Android but not WP. Assuming no issue with the server code, I would suspect either a key mismatch issue (perhaps the key got updated for some device codebases but not others) or a token validation problem. It's possible that this login call is attaching an older token which has expired. Please check to see if this is the case - a trace using Fiddler or similar may help you track this down. You might even use that to compare the call to one coming from Android. You can also use client.Logout() in advance will ensure that no token is sent to the login endpoint.