gpt4 book ai didi

c# - 如何使用 CSP 全局管理员凭据在 Azure AD 中为我的租户注册应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:25 25 4
gpt4 key购买 nike

我的目标是使用 C# 的 CSP 全局管理员帐户在 Azure Active Directory 中为我的租户创建应用程序。

因为它是通过 PowerShell 命令工作的。

Login-AzureRmAccount ==> CSP Global admin credentials
Select-AzureRmSubscription -TenantId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx(Enter your Customer Microsoft ID)" ==> Select a tenant where I want to create application
$password = ConvertTo-SecureString "SomePass@123" -asplaintext -force
New-AzureRmADApplication -DisplayName "MyApp" -HomePage "http://MyApp" -IdentifierUris "http://MyApp" -Password $password ==> Application created in the above mentioned tenants account.


请帮助我在 C# 中做同样的事情。

最佳答案

    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 快速为您创建了一个示例。您可以先尝试使用此代码片段。

Update: It is not suggested to hardcode your username and password. If you enable MFA, you may not able to get a token. If MFA is disabled, you can try with the follwoing code snippet:

    string userName = @"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d353535350d3535353563222320242e3f223e222b39632e2220" rel="noreferrer noopener nofollow">[email protected]</a>";
string passWord = @"password";

var context = new AuthenticationContext("https://login.microsoftonline.com/tenant_id");

result = context.AcquireTokenAsync(
resource,
clientid,
new UserPasswordCredential(userName, passWord)).Result;

关于c# - 如何使用 CSP 全局管理员凭据在 Azure AD 中为我的租户注册应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55609587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com