2016-08-12 9 views
1

私はazureアクティブディレクトリ認証にgoogleとmicrosoftアカウントを使用しています。 AADから認証した後、アクセストークンを返します。リクエストヘッダーを渡しています。トークンのベースでは、私は現在のプロバイダの詳細を取得したい。これは、ヘッダーで渡しているトークンの助けを借りてWeb APIコードのユーザーの詳細を取得することは可能ですか?Azureのアクティブディレクトリトークンを持つプロバイダの詳細

+0

、どのようなことは、このですか? – Biscuits

+0

WS-Federationを使用すると、SAMLトークンのクレームセットからIDプロバイダを取得できます。クレームタイプは「idp」と呼ばれます。 – Biscuits

+0

私はこのコードをxamarinフォームで使用しています。var user = DependencyService.Get ()。LoginAsync(azureService.MobileService、provider); "MobileServiceAuthencticationToken"でトークンを取得しています –

答えて

3

/.auth/meエンドポイントからすべての種類の情報を取得できます。あなたはXamarinフォームとAzureのモバイルアプリを使用しているので:

List<AppServiceIdentity> identities = null; 

public async Task<AppServiceIdentity> GetIdentityAsync() 
{ 
    if (client.CurrentUser == null || client.CurrentUser?.MobileServiceAuthenticationToken == null) 
    { 
     throw new InvalidOperationException("Not Authenticated"); 
    } 

    if (identities == null) 
    { 
     identities = await client.InvokeApiAsync<List<AppServiceIdentity>>("/.auth/me"); 
    } 

    if (identities.Count > 0) 
     return identities[0]; 
    return null; 
} 

プロバイダとプロバイダ:

using System.Collections.Generic; 
using Newtonsoft.Json; 

namespace TaskList.Models 
{ 
    public class AppServiceIdentity 
    { 
     [JsonProperty(PropertyName = "id_token")] 
     public string IdToken { get; set; } 

     [JsonProperty(PropertyName = "provider_name")] 
     public string ProviderName { get; set; } 

     [JsonProperty(PropertyName = "user_id")] 
     public string UserId { get; set; } 

     [JsonProperty(PropertyName = "user_claims")] 
     public List<UserClaim> UserClaims { get; set; } 
    } 

    public class UserClaim 
    { 
     [JsonProperty(PropertyName = "typ")] 
     public string Type { get; set; } 

     [JsonProperty(PropertyName = "val")] 
     public string Value { get; set; } 
    } 
} 

その後、次の主張を取得するために使用します。請求のためのモデルを設定し

トークンはモデル内にあります。プロバイダによって返されたすべての要求は、UserClaimsオブジェクトにあり、LINQを使用してアクセスできます。たとえば、名前取得する:あなたは私の本のセクションからより多くの情報を得ることができます

var identity = await service.GetIdentityAsync(); 
if (identity != null) { 
    name = identity.UserClaims.FirstOrDefault(c => c.Type.Equals("name")).Value; 
} 

を:アクセストークンのhttps://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/authorization/

+0

クライアント側で.auth/meを使用してユーザーのdetialを取得できることはわかっていますが、サーバー側でユーザーの詳細を取得したいのですが、ウェブAPIのトークンの助けを借りて。 –

+0

サーバー側については、こちらを参照してください。https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/#user-info –

+0

ありがとう、アドリアンホール。私はそれが働いていることを試みました。 –

関連する問題