簡體   English   中英

提供者的詳細信息與Azure活動目錄令牌

[英]Detail of Provider with Azure active directory token

我使用谷歌和微軟帳戶進行azure活動目錄身份驗證。 在從AAD進行身份驗證后,它返回訪問令牌。 我們正在傳遞請求標頭。 在令牌的基礎上,我想獲得當前提供商的詳細信息。 這可以通過標題中的標記幫助在web api代碼中獲取用戶的詳細信息嗎?

您可以從/.auth/me端點獲取各種信息。 由於您使用的是帶有Xamarin Forms的Azure移動應用程序:

為索賠建立模型:

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM