2017-08-08 7 views
0

私はthis exampleのMSALでAzure AD B2Cを設定しました。ユーザーはFacebookでログインすることができます。私はユーザーオブジェクトを手に入れることができます。 ここでは、Country、Cityなどの属性を取得したいと考えています。これらはAzure AD B2C属性で定義されています。Azure AD B2Cアプリケーション - グラフAPIの呼び出し方法

このためにGraph APIが必要ですか? graph.microsoft.comを呼び出そうとしましたが、トークンエラーが発生します。私はapp registrationを作成する必要がある記事を読んだが、Azure AD B2Cで作成されたアプリケーションIDの代わりにそのアプリケーションIDを使用すると、B2Cポータルで作成されたアプリケーションIDを使用する必要があるというエラーがスローされます。

アプリケーションでは、私はグラフAPIへのアクセスを設定することはできません。

おそらくこのロジックを実装できるC#APIがあります。それは行く方法ですか?理想的には私はクライアントから直接それをやりたい

これがどのように連携して動作するのは非常に混乱しています。私は同様の質問のほとんどを読んだが、同じ質問を見つけることができなかった。

答えて

1

少なくとも現在ログインしているユーザーの属性には、グラフAPIを必要としないアプローチがあります。 sign_inおよびsign_upポリシーを設定するとき、認証が成功した後でトークンを介して返されるクレームを構成できます。 この方法は、現在ログインしているユーザーのみに適用されます。他のユーザーに関する情報が必要な場合は、graphAPIを使用する必要があります。

あなたのアプリケーションでは、現在のClaimsPrincipalオブジェクトからそれらの要求/属性を取得できます。

たとえば、現在のアプリケーションで使用するクラスで、ユーザーのすべての必要な情報にアクセスすることができます。

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

インターフェースは私がそれを必要な場所私はそれを注入することができます::カスタムユーザー属性(PHONE_NUMBER)はさえあり

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

そしてそれは働きました!私はグラフAPIを使用せずに、コンソール内のすべてのプロパティ(アドレス、表示名など)を見ることができます。

関連する問題