要禁用AAD身份验证直线和团队时,可以使用以下代码示例:
// 导入所需的命名空间
using Microsoft.Graph;
using Microsoft.Identity.Client;
// 定义客户端ID和秘密
string clientId = "your_client_id";
string clientSecret = "your_client_secret";
string tenantId = "your_tenant_id";
// 创建身份验证提供程序
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithClientSecret(clientSecret)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.Build();
// 获取访问令牌
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
AuthenticationResult authenticationResult = await confidentialClientApplication
.AcquireTokenForClient(scopes)
.ExecuteAsync();
// 使用访问令牌创建 GraphServiceClient 实例
GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
return Task.FromResult(0);
}));
// 禁用安全码
await graphServiceClient.Me
.Request()
.UpdateAsync(new User
{
SecurityEnabledOnly = true
});
// 导入所需的命名空间
using System.Net.Http;
using System.Net.Http.Headers;
// 定义客户端ID和秘密
string clientId = "your_client_id";
string clientSecret = "your_client_secret";
string tenantId = "your_tenant_id";
// 创建 HttpClient
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetAccessToken());
// 获取访问令牌
string GetAccessToken()
{
string authority = $"https://login.microsoftonline.com/{tenantId}";
string resource = "https://graph.microsoft.com";
string scope = "https://graph.microsoft.com/.default";
string url = $"{authority}/oauth2/token";
List> requestData = new List>()
{
new KeyValuePair("grant_type", "client_credentials"),
new KeyValuePair("client_id", clientId),
new KeyValuePair("client_secret", clientSecret),
new KeyValuePair("resource", resource)
};
FormUrlEncodedContent content = new FormUrlEncodedContent(requestData);
HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
string responseString = response.Content.ReadAsStringAsync().Result;
JObject json = JObject.Parse(responseString);
return json["access_token"].ToString();
}
// 禁用安全码
string endpoint = "https://graph.microsoft.com/v1.0/me";
string payload = "{\"securityEnabledOnly\": true}";
HttpContent httpContent = new StringContent(payload);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage updateResponse = httpClient.PatchAsync(endpoint, httpContent).Result;
请注意,这些示例假设你已经正确设置了应用程序的权限和访问令牌,并正确配置了租户ID、客户端ID和客户端秘密。在实际使用时,请根据你的环境进行相应调整。
上一篇:AAD身份验证获取访问令牌
下一篇:AAD是否支持nonce声明?