2017-08-08 93 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。

相關問題