2016-08-12 77 views
1

我已經使用谷歌和微軟賬戶進行azure活動目錄驗證。從AAD認證後,它返回訪問令牌。我們正在傳遞請求標題。在令牌的基礎上,我想獲得當前提供者的詳細信息。這是可能得到用戶在Web API API的代碼的細節,在標題的幫助下,我通過標題?提供者與Azure活動目錄令牌的詳細信息

+0

什麼樣的訪問令牌是這樣嗎? – Biscuits

+0

使用WS-Federation,我可以從SAML令牌的聲明集中獲取身份提供者。索賠類型被稱爲'idp'。 – Biscuits

+0

我已經在xamarin表單中使用了這段代碼,var user = await DependencyService.Get ()。LoginAsync(azureService.MobileService,provider); 我在「MobileServiceAuthencticationToken」中獲取令牌 –

回答

3

您可以從/.auth/me端點獲取各種信息。由於您使用Azure的移動應用與Xamarin形式:

建立一個模型所聲稱的:

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; } 
    } 
} 

然後使用以下方法來獲取索賠:

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; 
} 

的供應商和供應商標記在模型中。任何由提供者返回的聲明都在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獲得用戶的詳細信息,但是我想在服務器端獲取用戶的詳細信息,我的意思是我想獲取詳細信息在web api中藉助令牌。 –

+0

服務器端記錄在這裏:https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/#user-info –

+0

謝謝阿德里安霍爾。我試過它正在工作。 –

相關問題