Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c根据Azure AD对用户的Pasword进行身份验证#_C#_Azure_Active Directory_Azure Active Directory_Azure Ad Graph Api - Fatal编程技术网

C# 如何使用c根据Azure AD对用户的Pasword进行身份验证#

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

我正在尝试使用用户名和密码获取令牌,但身份验证引发异常,且令牌为空。我对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;
}

试试看
{
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);
                }

            }