【问题标题】:How to Authenticate Pasword of User against Azure AD using c#如何使用 c# 针对 Azure AD 验证用户密码
【发布时间】:2020-02-10 19:51:50
【问题描述】:

我正在尝试使用用户名和密码获取令牌,但身份验证抛出异常并且令牌为 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# azure active-directory azure-active-directory azure-ad-graph-api


【解决方案1】:

这段代码没问题。您需要在 Azure 门户中检查配置。

1.将您的应用程序视为公共客户端。

2.添加 Azure 服务管理 api 权限并授予管理员/用户同意(此用户和资源的交互式授权请求。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多