使用Azure Active Directory身份验证而不重定向到Microsoft

时间:2019-01-22 22:02:05

标签: c# visual-studio azure asp.net-web-api azure-active-directory

是否可以仅将用户名和密码传递到Azure Active Directory并获取承载令牌,而不是将用户发送到login.microsoft.com网站?

重定向中断了我的Visual Studio调试,并使其无法查看正在发生的情况。

1 个答案:

答案 0 :(得分:1)

不太清楚您的确切设置是什么。但是,可以使用“密码授予”直接获取Bearer令牌。

我只建议出于测试目的。在生产方案中使用此赠款仅应考虑用于遗留目的。

https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Username-Password-Authentication

{
    "error": {
        "root_cause": [{
            "type": "query_shard_exception",
            "reason": "Failed to parse query [*http://localhost:8000/v3/assets/blt5ed2da305d61b2a2/blt10e8f794fa8597d0/5c481035fecde97033721922/Screenshot_from_2018-07-30_09-53-42.png*]",
            "index_uuid": "ytCEFbRQSmOaIaCXA5Q7LQ",
            "index": "test"
        }],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [{
            "shard": 0,
            "index": "test",
            "node": "EVRoeO0DTIOQKf95TeA_7g",
            "reason": {
                "type": "query_shard_exception",
                "reason": "Failed to parse query [*http://localhost:8000/v3/assets/blt5ed2da305d61b2a2/blt10e8f794fa8597d0/5c481035fecde97033721922/Screenshot_from_2018-07-30_09-53-42.png*]",
                "index_uuid": "ytCEFbRQSmOaIaCXA5Q7LQ",
                "index": "test",
                "caused_by": {
                    "type": "parse_exception",
                    "reason": "Cannot parse '*http://localhost:8000/v3/assets/blt5ed2da305d61b2a2/blt10e8f794fa8597d0/5c481035fecde97033721922/Screenshot_from_2018-07-30_09-53-42.png*': Encountered \" \":\" \": \"\" at line 1, column 5.\nWas expecting one of:\n    <EOF> \n    <AND> ...\n    <OR> ...\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    \"^\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <FUZZY_SLOP> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    <REGEXPTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    ",
                    "caused_by": {
                        "type": "parse_exception",
                        "reason": "Encountered \" \":\" \": \"\" at line 1, column 5.\nWas expecting one of:\n    <EOF> \n    <AND> ...\n    <OR> ...\n    <NOT> ...\n    \"+\" ...\n    \"-\" ...\n    <BAREOPER> ...\n    \"(\" ...\n    \"*\" ...\n    \"^\" ...\n    <QUOTED> ...\n    <TERM> ...\n    <FUZZY_SLOP> ...\n    <PREFIXTERM> ...\n    <WILDTERM> ...\n    <REGEXPTERM> ...\n    \"[\" ...\n    \"{\" ...\n    <NUMBER> ...\n    "
                    }
                }
            }
        }]
    },
    "status": 400
}

您还可以使用Postman或类似工具:

string authority = "https://login.microsoftonline.com/contoso.com";
string[] scopes = new string[] { "user.read" };
PublicClientApplication app = new PublicClientApplication(clientId, authority);


    try
    {
        var securePassword = new SecureString();
        foreach (char c in "dummy")        // you should fetch the password
            securePassword.AppendChar(c);  // keystroke by keystroke

        result = await app.AcquireTokenByUsernamePasswordAsync(scopes, "joe@contoso.com",
                                                               securePassword);
    }
    catch(MsalException)
    {
      // See details below
    }

Console.WriteLine(result.Account.Username);