Monday, 11 March 2019

Error: “unexpected error retrieving the credentials” while fetching user claims in Azure Mobile App


Introduction


Azure Mobile Apps uses tokens to authenticate users. Tokens are pieces of encoded data that contain information about a user. 
Whenever a user signs in with one of the built-in authentication mechanisms, a token is generated by the service that uniquely identifies the user.

This token is then retrieved by the client application. The client application provides this token with every request to the service. When a service request is processed, the Mobile App service examines the supplied token and validates it - if the token is valid the request is allowed and processed; if not, it returns 401 Unauthorized error.

If you have recently created an Azure mobile app, which configured to authenticate using Azure Active Directory sign-in as directed in this documentation - https://docs.microsoft.com/en-gb/azure/app-service/app-service-mobile-how-to-configure-active-directory-authentication?toc=%2fazure%2fapp-service-mobile%2ftoc.json.

You might experience issues with the following line of code in the IPrincipal extension class of azure mobile app service -

AzureActiveDirectoryCredentials aadCreds = await this.User.GetAppServiceIdentityAsync(this.Request);

This line of code is used to fetch the user claims (login credentials) from the X-ZUMO-AUTH in which the client application has presented the authentication code.


Error Message


"Message": "There was an unexpected error retrieving the credentials: 'StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n  X-Content-Type-Options: nosniff\r\n  x-ms-request-id: eb06e72b-2c22-4b94-9118-231b01311500\r\n  Cache-Control: private\r\n  Date: Thu, 31 Jan 2019 12:41:30 GMT\r\n  P3P: CP=\"DSP CUR OTPi IND OTRi ONL FIN\"\r\n  Set-Cookie: x-ms-gateway-slice=prod; path=/; secure; HttpOnly\r\n  Server: Microsoft-IIS/10.0\r\n  Content-Length: 0\r\n}'

Fix


Microsoft has accepted this as bug. 

If an application has the site auth setting runtimeVersion set to any valid version >= 1.0.0, instead of populating the user Claims Principal with the claims from the X-ZUMO-AUTH token, it will populate them based on the claims in the token store (i.e. the claims from the original aad access token). This site auth setting started defaulting to "1.0.0" when turning on EasyAuth around the same time, which why we are only seeing this behavior on newer applications.

As a workaround, you can set "runtimeVersion" to the empty string or remove it entirely using https://resources.azure.com/. You can find it via the following path: subscriptions > (site subscription) > resource groups > (site resource group) > providers > Microsoft.Web > sites > (site name) > config > authsettings. Then you can find "runtimeVersion" under the "properties" field of the json.


This should resolve the error.



No comments:

Post a Comment