c# - 如何使用 c# 针对 Azure AD 验证用户密码
问题描述
我正在尝试使用用户名和密码获取令牌,但身份验证抛出异常并且令牌为 NULL 。我对 Azure 很陌生。我的代码:
public string AuthenticateUser(string username, string password)
{
const string resources = "https://management.core.windows.net/";
const string clientId = "";
const string aadTokenIssuerUri = "https://login.windows.net/common/";
AuthenticationContext authenticationContext = new AuthenticationContext(aadTokenIssuerUri);
UserCredential userCredentials = new UserPasswordCredential(username, password);
if (authenticationContext.TokenCache != null)
{
authenticationContext.TokenCache.Clear();
}
AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(
resources,
clientId,
userCredentials).GetAwaiter().GetResult();
var token = authenticationResult.AccessToken;
return token;
}
try
{
var token = azureUsers.AuthenticateUser("", "");
if (!string.IsNullOrEmpty(token))
{
Console.WriteLine("here is token {0}", token);
result = true;
}
}
catch (Exception e)
{
if (i < numRetries)
{
Thread.Sleep(retryInterval);
}
}
请告诉我这段代码有什么问题。
解决方案
推荐阅读
- c++ - 如何从 Qt Creator 保存和导出自定义构建步骤?
- python - 两个日期之间的天数和索引
- postgresql - 所有表和视图的 postgrest swagger-ui 设置文档,而不授予匿名用户对表和视图的选择权限
- firebase - 在进行身份验证之前,如何使用 Flutter 从 Cloud Firestore 搜索和访问数据?
- flutter - 使用 Flutter Form Builder Package 实现 Stepper Widget
- mysql - Laravel 迁移错误“无法添加外键约束”
- sql-server - Powershell 使用 System.Data.SqlDBType varbinary
- javascript - 为什么 .swiper-button-prev 和 .swiper-button-next 不起作用?
- javascript - 如何使用 NextJS 只使用一次动态导入
- android - Android 10 - 在 Acrobat 中打开具有写入权限的 PDF