使用用户名和密码获取 Azure Active Directory 令牌

Muh*_*awi 5 active-directory azure azure-active-directory

我正在尝试使用 AAD 对我的客户端进行身份验证,并使用 Windows 服务自动执行此操作。在 AAD .NET SDK 中,有两种方法AcquireTokenAsyncAcquireToken,但我不能使用这两种方法中的任何一种,等待调用将永远停留而没有响应,当我执行以下操作时:

result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
Run Code Online (Sandbox Code Playgroud)

该对象返回状态Waiting for Activation& Code 31..

现在,是否可以使用硬编码的用户名和密码获取令牌?

我的完整代码:

        string hardcodedUsername = "username";
        string hardcodedPassword = "password";

        string tenant = "tenantId@onmicrosoft.com";
        string clientId = "clientId";
        string resourceHostUri = "https://management.azure.com/";
        string aadInstance = "https://login.microsoftonline.com/{0}";

        string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);


        authContext = new AuthenticationContext(authority);

        AuthenticationResult result = null;
            try
            {

                result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            return result;
Run Code Online (Sandbox Code Playgroud)

我正在尝试访问 Azure API。

更新1:

当我尝试await拨打电话时,我在输出中得到了这个,我认为这可能会有所帮助:

Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache:查找令牌的缓存... Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache:在缓存中未找到匹配的令牌 Microsoft.IdentityModel.Clients.ActiveDirectory d__0:将用户领域发现请求发送到' https://login.microsoftonline.com/common/UserRealm/ username ?api-version=1.0' Microsoft.IdentityModel.Clients.ActiveDirectory d__4:具有哈希“***”的用户被检测为“联合”

小智 1

尝试下面的链接代码

https://msdn.microsoft.com/en-in/library/partnercenter/dn974935.aspx

如何在 Windows Azure Active Directory 身份验证后获取访问令牌

如何从 Azure ActiveDirectory 应用程序获取当前令牌

// Get OAuth token using client credentials 
string tenantName = "GraphDir1.OnMicrosoft.com";
string authString = "https://login.microsoftonline.com/" + tenantName;

AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);

// Config for OAuth client credentials  
string clientId = "118473c2-7619-46e3-a8e4-6da8d5f56e12";
string key = "hOrJ0r0TZ4GQ3obp+vk3FZ7JBVP+TX353kNo6QwNq7Q=";
ClientCredential clientCred = new ClientCredential(clientId, key);
string resource = "https://graph.windows.net";
string token;
try
{
    AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred);
    token = authenticationResult.AccessToken;
}
catch (AuthenticationException ex)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Acquiring a token failed with the following error: {0}", ex.Message);
    if (ex.InnerException != null)
    {
        //  You should implement retry and back-off logic according to
        //  http://msdn.microsoft.com/en-us/library/dn168916.aspx . This topic also
                                //  explains the HTTP error status code in the InnerException message. 
        Console.WriteLine("Error detail: {0}", ex.InnerException.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 有两件事:1) OP 希望使用用户名/密码组合而不是使用客户端凭据来获取凭据,2) OP 使用异步方法。您所显示的是同步方法,并且您正在使用客户端凭据。 (2认同)