rav*_*hod 8 c# azure-active-directory
这就是我编写代码并尝试获取输出的方法.
请求正文必须包含以下参数:
client_secret或client_assertion
static async Task<AuthenticationResult> getAccessToken()
{
string hardcodedUsername = "";
string hardcodedPassword = "";
string tenantName = "projectwidgets.com";
string authString = "https://login.microsoftonline.com/" + tenantName;
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
//Config for OAuth client credentials
string clientId = "as";
string key = "kk";
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenantName);
var authContext = new AuthenticationContext(authority);
AuthenticationResult result = null;
try
{
result = await authContext.AcquireTokenAsync("https://pwsnapitazure.azurewebsites.net", clientId, new UserPasswordCredential(hardcodedUsername, hardcodedPassword));
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
由于 Azure应用注册 UI 已从旧版. 所以你需要启用名为的添加设置
默认客户端类型为是
在Manifest 中,您也可以控制这将
"allowPublicClient": true
Run Code Online (Sandbox Code Playgroud)
希望这对某人有所帮助
根据您的代码,您似乎正在使用使用用户名和密码进行身份验证的Web应用程序/ API.
我们只能使用本机客户端的资源所有者流.机密客户端(例如网站)无法使用直接用户凭据.
您需要将其作为公共客户端(本机客户端应用程序)调用,而不是作为机密客户端(Web应用程序/ API).有关如何使用ADAL .NET通过用户名/密码验证用户的更多信息,请参阅此文档Constraints & Limitations.特别是本节.
在守护程序或服务器应用程序中,您可以考虑使用客户端凭据流,但是使用此流程,应用程序将其客户端凭据提供给OAuth2令牌发出端点,并且作为回报,获取代表应用程序本身的访问令牌,而无需任何用户信息.请点击这里关于客户端凭证流的详细信息,并在这里是代码样本.
| 归档时间: |
|
| 查看次数: |
9433 次 |
| 最近记录: |