C# 如何使用c根据Azure AD对用户的Pasword进行身份验证#
我正在尝试使用用户名和密码获取令牌,但身份验证引发异常,且令牌为空。我对Azure还很陌生。我的代码:C# 如何使用c根据Azure AD对用户的Pasword进行身份验证#,c#,azure,active-directory,azure-active-directory,azure-ad-graph-api,C#,Azure,Active Directory,Azure Active Directory,Azure Ad Graph Api,我正在尝试使用用户名和密码获取令牌,但身份验证引发异常,且令牌为空。我对Azure还很陌生。我的代码: public string AuthenticateUser(string username, string password) { const string resources = "https://management.core.windows.net/"; const string clientId = ""; const str
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;
}
试试看
{
var-token=azureUsers.AuthenticateUser(“,”);
如果(!string.IsNullOrEmpty(令牌))
{
WriteLine(“这里是令牌{0}”,令牌);
结果=真;
}
}
捕获(例外e)
{
如果(i
请告诉我此代码有什么问题。此代码没有问题。您需要检查Azure portal中的配置 1.将您的应用程序视为公共客户端 2.添加Azure服务管理api权限并授予管理员/用户许可(此用户和资源的交互式授权请求。)
此代码很好。您需要检查Azure portal中的配置 1.将您的应用程序视为公共客户端 2.添加Azure服务管理api权限并授予管理员/用户许可(此用户和资源的交互式授权请求。)
看看答案。如果这仍然不能解决您的问题,您可以在此处提供异常消息。查看答案。如果这仍然不能解决您的问题,您可以在此处提供异常消息。
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);
}
}