public static string postRequest(string url, string access_token, string data)
{
byte[] buffer = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "post";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + access_token);
//request.Headers.Add("other header", "it's value");
if (data != null)
buffer = Encoding.UTF8.GetBytes(data);
else
buffer = Encoding.UTF8.GetBytes("");
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return response.StatusCode + " " + reader.ReadToEnd();
}
}
public class PasswordCredential
{
public string startDate;
public string endDate;
public string keyId;
public string value;
}
public class AppConfiguration
{
public bool availableToOtherTenants;
public string displayName;
public string homepage;
public List<string> identifierUris = new List<string>();
public List<PasswordCredential> passwordCredentials = new List<PasswordCredential>();
}
static void Main(string[] args)
{
string tenantId = @"customer tenant id";
string resource = @"https://graph.windows.net/";
string clientId = @"1950a258-227b-4e31-a9cf-717495945fc2";
string returnUri = @"urn:ietf:wg:oauth:2.0:oob";
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
var uri = new Uri(returnUri);
var platformParams = new PlatformParameters(PromptBehavior.Always);
var authResult = context.AcquireTokenAsync(resource, clientId, uri, platformParams).Result;
var accessToken = authResult.AccessToken;
var url = @"https://graph.windows.net/{customer_tenant_id}/applications?api-version=1.6";
var passwordCredential = new PasswordCredential();
passwordCredential.startDate = DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ssZ");
passwordCredential.endDate = DateTime.UtcNow.AddYears(1).ToString("yyyy-MM-ddThh:mm:ssZ");
passwordCredential.keyId = Guid.NewGuid().ToString();
passwordCredential.value = "TestPassword1.";
var appConfiguration = new AppConfiguration();
appConfiguration.availableToOtherTenants = false;
appConfiguration.displayName = "MyApp";
appConfiguration.homepage = "Https://MyApp";
appConfiguration.identifierUris.Add("https://MyApp");
appConfiguration.passwordCredentials.Add(passwordCredential);
var body = JsonConvert.SerializeObject(appConfiguration);
//Console.WriteLine(body);
var result = postRequest(url, accessToken, body);
Console.WriteLine(result);
Console.ReadLine();
}
Я быстро создал образец для вас, используя ADAL, Newtonsoft.Json и HttpWebRequest.Вы можете сначала попробовать этот фрагмент кода.
Обновление: Не рекомендуется жестко кодировать ваше имя пользователя и пароль.Если вы включите MFA, вы не сможете получить токен.Если MFA отключен, вы можете попробовать следующий фрагмент кода:
string userName = @"xxxx@xxxx.onmicrosoft.com";
string passWord = @"password";
var context = new AuthenticationContext("https://login.microsoftonline.com/tenant_id");
result = context.AcquireTokenAsync(
resource,
clientid,
new UserPasswordCredential(userName, passWord)).Result;