【问题标题】:Detail of Provider with Azure active directory token具有 Azure 活动目录令牌的提供程序的详细信息
【发布时间】:2016-08-13 00:56:40
【问题描述】:

我使用 google 和 microsoft 帐户进行 Azure Active Directory 身份验证。从 AAD 进行身份验证后,它会返回访问令牌。我们正在传递请求标头。基于令牌,我想获取当前提供者的详细信息。这是否可以借助我在标头中传递的令牌来获取 Web api 代码中的用户详细信息?

【问题讨论】:

  • 这是什么访问令牌?
  • 使用 WS-Federation,我能够从 SAML 令牌的声明集中获取身份提供者。声明类型称为“idp”。
  • 我在 xamarin 表单中使用了这段代码,var user = await DependencyService.Get().LoginAsync(azureService.MobileService, provider);我在“MobileServiceAuthencticationToken”中获得令牌

标签: c# azure-mobile-services


【解决方案1】:

您可以从/.auth/me 端点获取各种信息。由于您将 Azure 移动应用与 Xamarin Forms 一起使用:

为索赔建立模型:

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/

【讨论】:

  • 我知道我们可以在客户端使用 .auth/me 获取用户的详细信息,但我想在服务器端获取用户的详细信息,我的意思是我想在 Web 中获取详细信息api 借助 token。
猜你喜欢
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2019-12-31
  • 1970-01-01
相关资源
最近更新 更多