Eric Regnier
Eric Regnier

Reputation: 21

Get authenticated user information in Azure Api App

Context

I have a Azure Api App set as "Public (authenticated)" and have enabled Facebook identity. My Api App is protected and I am able authenticated with FB. In the Api App, I want to get the authenticated user information such as login provider, token and email to save in a database.

Problem

I can't find any documentation on how to obtain this info. I followed this article and tried

    var runtime = Runtime.FromAppSettings(Request);
    var user = runtime.CurrentUser;
    TokenResult token = user.GetRawTokenAsync("facebook").Result;
    var name = (string)token.Claims["name"];
    var email = (string)token.Claims["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn"];

I've made the changes to the apiapp.json file to allow facebook authentication "authentication": [{"type": "facebook"}] but it times out at GetRawTokenAsync when remote debugging the Api App.

Looking at other blogs/articles/posts, following is another way by referencing additional assemblies "Microsoft.WindowsAzure.Mobile"

    var user = (MobileAppUser)this.User;
    var facebookCredentials = user.GetIdentityAsync<FacebookCredentials>().Result;

But this also doesn't work as this.user is of type WindowsPrincipal user when remote debugging my Api App. And apparently "Microsoft.Azure.AppService.ApiApps.Service" is the only reference an Azure Api App needs.

Am I missing something? Seems like a basic scenario to get the authenticated user in a Api App web service.

Upvotes: 1

Views: 291

Answers (1)

Eric Regnier
Eric Regnier

Reputation: 21

Finally got it to work. When calling GetRawTokenAsync method we must await. Using Result or Wait from the Task object doesn’t work and times out. This is still unclear to me why, perhaps a framework bug?

Upvotes: 1

Related Questions