【发布时间】:2021-08-17 09:09:46
【问题描述】:
我有一个调用 OAuth 2.0 来获取访问令牌的 Web Api
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("client_id", _configuration["ClientId"]),
new KeyValuePair<string, string>("grant_type", "refresh_token"),
new KeyValuePair<string, string>("refresh_token", _configuration["RefreshToken"]),
new KeyValuePair<string, string>("scope", "openid Mail.Send offline_access")
});
var tenantId = _configuration["TenantId"];
try
{
var response = await _httpClient.PostAsync($"{tenantId}/oauth2/v2.0/token", content);
var status = (int)response.StatusCode;
if (status == 200)
{
var responseString = await response.Content.ReadAsStringAsync();
var refreshTokenResponse = JsonSerializer.Deserialize<RefreshTokenResponse>(responseString);
return refreshTokenResponse.AccessToken;
}
throw new Exception("Status " + status + " Content " + response?.Content +
" ReasonPhrase " + response.ReasonPhrase +
" RequestMessage " + response.RequestMessage );
当我在本地运行它时,即使没有范围,这段代码也能正常工作。相同的代码在部署到 Azure(Azure 应用服务)时会失败。即使是邮递员电话,无论有没有范围都可以正常工作。
响应包含以下消息 {"error":"invalid_grant","error_description":"AADSTS53003: 条件访问策略已阻止访问。访问策略不允许颁发令牌
不清楚为什么当我从本地机器和邮递员直接调用而不是从 Azure 应用服务进行调试时它会起作用。
我们将不胜感激任何帮助
【问题讨论】:
-
您是否在 Azure 门户:Azure AD 租户 > 条件访问中查看了可能会干扰(已部署)Web 应用程序的策略?
标签: azure oauth-2.0 azure-active-directory azure-web-app-service