2017-04-19 15 views
1

私は、azureアクティブディレクトリにプログラムでユーザーまたはグループを作成する必要があります。私はGoogleで検索し、グラフAPI、C#コードなどを使用するような複数のソリューションを見つけましたが、アプローチと少し混乱しています。ASP.Net(C#)を使用してAzure Active Directoryでグループ/ユーザを作成するには?

これらのアプローチの違いを私に教えてもらえますか?ご利用可能なコードサンプルがある場合はお知らせください。

ありがとうございます!

答えて

1

アズール広告サポートmultiple protocols。 Azure ADグラフのトークンを取得するには、Azure ADと対話するためにOAuth 2.0/OpenId connectで適切なフローを選択する必要があります。

たとえば、ウェブアプリケーションを開発している場合は、OAuthコードの許可フローが良い選択です。アプリがデーモンアプリまたはサービスアプリケーションの場合は、クライアントの資格情報フローがより優れています。シナリオの詳細についてはthis documentを参照してください。

ウェブアプリケーションでAzure AD Graphのトーク​​ンを取得するには、this code sampleを参照してください。このコードサンプルのline of 104でAzure AD Graphのアクセストークンを取得します。そして、コントローラでは、あなたがキャッシュからトークンを取得し、AzureのADグラフを使用してユーザーを作成するには、以下のコードを使用することができます:

string graphResourceId = "https://graph.windows.net"; 
string tenantId = "xxx.onmicrosoft.com"; 
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/xxx.onmicrosoft.com"); 
ClientCredential credential = new ClientCredential("{clientId}", "{secret}"); 
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 
AuthenticationResult result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 
var accessToken = result.AccessToken; 


Uri servicePointUri = new Uri(graphResourceId); 
Uri serviceRoot = new Uri(servicePointUri, tenantId); 

ActiveDirectoryClient graphClient = new ActiveDirectoryClient(serviceRoot, async() => await Task.FromResult(accessToken)); 

var user = new User(); 
user.AccountEnabled = true; 
user.DisplayName = "testName"; 
user.UserPrincipalName = "[email protected]"; 
user.MailNickname = "testName"; 
user.UsageLocation = "US"; 
user.PasswordProfile = new PasswordProfile 
{ 
    Password = "xxxxxx", 
    ForceChangePasswordNextLogin = true 
}; 

await graphClient.Users.AddUserAsync(user); 

とアプリケーションは、ユーザとグループを作成するにはDirectory.ReadWrite.Allが必要です。許可の詳細についてはhereを参照してください。

+0

おかげです。それは本当に私を助けてくれました。もう一度ありがとう :-) –

関連する問題