2017-08-08 94 views
0

我已经设置了MSAL的Azure AD B2C,服务对象为this example。用户可以用Facebook登录,我得到一个用户对象,都很好。 现在我想要检索属性,如Country,City等。这些属性在Azure AD B2C属性中定义。Azure AD B2C应用程序 - 如何调用Graph API?

我需要Graph API吗?我试过调用graph.microsoft.com,但我得到令牌错误。我已经阅读了我需要创建app registration的文章,但是当我使用该应用程序ID而不是在Azure AD B2C中创建的应用程序ID时,它会引发一个错误,我需要使用在B2C门户下创建的应用程序ID。

在B2C中的应用程序中,我无法配置我想要访问Graph API。

我们确实有一个C#API,我可以在那里实现这个逻辑。这是要走的路吗?理想情况下,我想直接从客户那里做。

这是如此合作的一切都非常混乱。我读过大部分类似的问题,但找不到同样的问题。

回答

1

至少对于当前登录用户的属性,有一种方法不需要Graph API。当您配置sign_insign_up策略时,您可以配置在认证成功后通过令牌返回哪些声明。 请注意,此方法仅适用于当前登录的用户。如果你需要其他用户的信息,你应该使用graphAPI。

基本上在您的应用程序中,您可以从当前的ClaimsPrincipal对象中检索这些声明/属性。

例如,这里是一个类,我在当前的应用程序中使用该类,并允许我访问用户的所有必需信息。甚至有一个自定义的用户属性(PHONE_NUMBER):

public class ApplicationUser : IApplicationUser 
{ 
    private readonly IHttpContextAccessor contextAccessor; 

    public ApplicationUser(IHttpContextAccessor contextAccessor) 
    { 
     this.contextAccessor = contextAccessor; 
    } 

    public virtual bool IsAuthenticated => contextAccessor.HttpContext?.User?.Identity.IsAuthenticated ?? false; 

    public string Id => GetAttribute(ClaimTypes.NameIdentifier); 
    public string GivenName => GetAttribute(ClaimTypes.GivenName); 
    public string Surname => GetAttribute(ClaimTypes.Surname); 
    public string DisplayName => GetAttribute("name"); 
    public bool IsNewUser => GetBoolAttribute("newUser"); 
    public string Email => GetAttribute("emails"); 
    public string PhoneNumber => GetAttribute("extension_PhoneNumber"); 

    public bool IsCurrentUser(string userId) 
    { 
     return userId != null && Id != null && userId.Equals(Id, StringComparison.CurrentCultureIgnoreCase); 
    } 

    private string GetAttribute(string claim) 
    { 
     return IsAuthenticated ? contextAccessor.HttpContext.User.GetClaim(claim) : null; 
    } 

    public bool GetBoolAttribute(string claim) 
    { 
     bool output; 
     bool.TryParse(GetAttribute(claim), out output); 
     return output; 
    } 
} 

public static class ClaimsPrincipalExtensions 
{ 
    public static string GetClaim(this ClaimsPrincipal principal, string claim) 
    { 
     if (principal == null) 
     { 
      throw new ArgumentNullException(nameof(principal)); 
     } 

     if (string.IsNullOrWhiteSpace(claim)) 
     { 
      throw new ArgumentNullException(nameof(claim)); 
     } 

     return principal.FindFirst(claim)?.Value; 
    } 
} 

该接口允许我到哪里,我需要它注入它:

public interface IApplicationUser 
{ 
    string Id { get; } 
    string GivenName { get; } 
    string Surname { get; } 
    string DisplayName { get; } 
    bool IsNewUser { get; } 
    string Email { get; } 
    string PhoneNumber { get; } 

    bool IsCurrentUser(string userId); 
} 

编辑:您可以很容易地通过创建将此信息传输到客户端一个休息api端点并用ajax调用它。或者用html中的数据属性传递它。

0

@vibronet和@regnauld的答案帮了很大忙!我已将登录/注册策略配置为拥有声明中的属性。但是使用MSAL JS时的访问令牌是加密的。于是,只好用this example到未加密的:

private setAuthenticated(accessToken: string) { 
    // Update the UI. 
    this.isAuthenticated = true; 
    this.identity = Msal.Utils.extractIdToken(accessToken) as AuthIdentity; 
    this.identity.displayName = this.identity.given_name + ' ' + this.identity.family_name; 
    console.log(this.identity); 
} 

然后它的工作!我可以在控制台中看到所有属性(地址,显示名称等),而无需使用Graph API。

相关问题