2016-10-23 9 views
0

ログインユーザー、ADユーザー名を取得するためにクエリに何を書き込むか教えていただけますか? ! GivenName、FamilyName、displayname ---- 私はドメイン** me.username **が必要です。ドメインを持っているので、ユーザー名だけです。 これは完全に動作し、ユーザーのADグループを取得するため、ユーザー名を取得するために何を変更する必要がありますか。現在のユーザーのAzure AD Graph APIを使用してログイン/ユーザー名を取得する方法

public async Task<List<Groups>> GetAdGroups(string authority, string resource, string clientId, string returnUri) 
    { 
     JObject jResult = null; 
     AuthenticationResult authResult = null; 
     var authContext = new AuthenticationContext(authority); 
     if (authContext.TokenCache.ReadItems().Any()) 
     { 
      authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); 
     } 
     var platformParams = new PlatformParameters((Activity)Forms.Context); 
     authResult = await authContext.AcquireTokenAsync(resource, clientId, new Uri(returnUri), platformParams); 
     var uri = new Uri(returnUri); 
     string graphRequest = String.Format(CultureInfo.InvariantCulture, "{0}/{1}/users/{2}/memberOf?api-version=1.5", ServiceConstants.graphResourceUri, ServiceConstants.tenant, authResult.UserInfo.UniqueId); 
     HttpClient client = new HttpClient(); 
     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, graphRequest); 
     request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); 

     HttpResponseMessage response = await client.SendAsync(request); 
     List<Groups> memberOf = new List<Groups>(); 
     string content = await response.Content.ReadAsStringAsync(); 
     jResult = JObject.Parse(content); 

     if (jResult["odata.error"] != null) 
     { 
      memberOf.Add(new Groups { id = "Error", displayName = (string)jResult["odata.error"]["message"]["value"] }); 
      return memberOf; 
     } 
     if (jResult["value"] == null) 
     { 
      memberOf.Add(new Groups { id = "Error", displayName = "Unknown Error." }); 
      return memberOf; 
     } 
     foreach (JObject result in jResult["value"]) 
     { 
      memberOf.Add(
       new Groups 
       { 
        id = (string)result["objectId"], 
        displayName = (string)result["displayName"] 
       }); 
     } 
     return memberOf; 
    } 

誰かが助けることを望みます。敬具、キリスト教徒。

+0

authContext(https://jwt.ioを確認)もデコードできます。これにはユーザー名が含まれるためです。 – RasmusW

答えて

0

Azure ADアカウントのログイン名を取得するには、userPrincipalNameをユーザーオブジェクトから取得できます。以下はこのプロパティの説明ですEntity and complex type referenceです。

UPNは、インターネット標準のRFC 822に基づくユーザーのインターネットスタイルのログイン名です。これは、慣例として、ユーザーの電子メール名に対応する必要があります。一般的な書式は "alias @ domain"です。職場または学校のアカウントの場合、ドメインはテナントの検証済みドメインのコレクションに存在する必要があります。このプロパティは、職場または学校のアカウントを作成するときに必要です。ローカルアカウントではオプションです。

そしてここでは、あなたの参考のために、このプロパティを取得するためにAzureのグラフクライアントを使用してコードです:

//client is the instance of ActiveDirectoryClient 
client.Me.ExecuteAsync().Result.UserPrincipalName 

そして、あなたはあなたが以下の要求を参照することができ、直接AzureのグラフRESTを呼び出した場合:

GET: https://graph.windows.net/me?api-version=1.6 
+0

ありがとう、私はそれを@で分割して、すべてはOKです – LuckyLyck

関連する問題