是否可以简单地将用户名和密码传递到 Azure Active Directory 并取回不记名 token ,而不是将用户发送到 login.microsoft.com 网站?
重定向破坏了我的 Visual Studio 调试并导致无法查看发生了什么。
最佳答案
不太确定您的确切设置是什么。但是,可以使用密码授予直接获取承载 token 。
我仅建议将此用于测试目的。在生产场景中使用此授权只能用于遗留目的。
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, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6aca9a386a5a9a8b2a9b5a9e8a5a9ab" rel="noreferrer noopener nofollow">[email protected]</a>",
securePassword);
}
catch(MsalException)
{
// See details below
}
Console.WriteLine(result.Account.Username);
您还可以使用 Postman 或类似工具:
curl -X POST \
https://login.windows.net/<tenant>/oauth2/token \
-H 'Accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=password&scope=openid&resource=<resourceUri>&client_id=<yourClientId>&client_secret=<clientSecret>&username=<username>&password=<password>'
关于c# - 使用 Azure Active Directory 身份验证而不重定向到 Microsoft,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54317112/