【问题标题】:Azure AD - HTTP/1.1 401 Unauthorized - Windows ServiceAzure AD - HTTP/1.1 401 未经授权 - Windows 服务
【发布时间】:2023-03-23 12:53:01
【问题描述】:

Azure 身份验证存在问题。调用 Azure 中托管的 WebAPI 时出现 401 Unauthorized。

以下是基础知识: 遵循本教程 - https://azure.microsoft.com/en-us/documentation/samples/active-directory-dotnet-daemon/

已注册 2 个应用程序:

  1. Windows 服务
  2. WebAPI

Windows 服务调用 STS 并成功获取 JWT。然后它会尝试将该令牌传递给托管在 azurewebsites.net 上的 WebAPI。

这是我调用 STS 的代码(只是包装在 Auth 类中):

public class AzureAuth
{

    private AuthenticationContext authContext;
    private ClientCredential clientCredential;

    protected string AzureADInstanceName;
    protected string AzureADTenancyName;

    protected string AzureADApplicationClientId;
    protected string AzureADApplicationSecret;
    protected string AzureADApplicationAppId;


    public AzureAuth(string azureADInstanceName, string azureADTenancyName, string azureADApplicationClientId, string azureADApplicationSecret, string azureADApplicationAppId)
    {
        this.AzureADInstanceName = azureADInstanceName;
        this.AzureADTenancyName = azureADTenancyName;
        this.AzureADApplicationClientId = azureADApplicationClientId;
        this.AzureADApplicationSecret = azureADApplicationSecret;
        this.AzureADApplicationAppId = azureADApplicationAppId;

        string authority = string.Format(CultureInfo.InvariantCulture, AzureADInstanceName, AzureADTenancyName);
        this.authContext = new AuthenticationContext(authority);
        this.clientCredential = new ClientCredential(AzureADApplicationClientId, AzureADApplicationSecret);
    }

    public async Task ExecuteGetWithAADAutToken<T>(Action<AuthenticationResult> AfterAuthAction)
    {
        AuthenticationResult result = null;
        try
        {
            result = await authContext.AcquireTokenAsync(this.AzureADApplicationAppId, this.clientCredential);
            AfterAuthAction(result);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}

然后我使用该 Auth 类和 AuthResult 来调用 WebApi:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(getEndPointUri);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", res.AccessToken);

    HttpResponseMessage response = await client.GetAsync("/api/field");

    if (response.IsSuccessStatusCode)
    {
        var resultStringJSON = await response.Content.ReadAsStringAsync();
        Console.WriteLine(resultStringJSON);
    }
    else
    {
        var resulterrorStringJSON = await response.Content.ReadAsStringAsync();
        Console.WriteLine(resulterrorStringJSON);
    }
}

HttpResponseMessage 响应是未经授权的

我使用 fiddler 来获取请求和响应:

GET https://xxxxxxxx.azurewebsites.net/api/someapi HTTP/1.1
Accept: application/json
Authorization: Bearer XXXXXXXXX
Host: xxxxxx.azurewebsites.net


HTTP/1.1 401 Unauthorized
Content-Length: 0
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer realm="xxxxxxx.azurewebsites.net"
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=697493f3974009aeb448f562d1b389f79aa6008071be1244c77f3f1d3849f5f0;Path=/;Domain=xxxxxxxx.azurewebsites.net
Date: Tue, 24 May 2016 03:59:15 GMT

请帮忙?

我怀疑的主要问题是,在 Azure 中,AAD 应用程序无法访问 Web API 应用程序。我尝试通过用户添加它,但是当我以用户身份搜索该应用程序时,它找不到它。注册的应用程序位于不同的 Azure AD 目录中。

【问题讨论】:

  • 您是否在门户中启用了身份验证/授权?有几个人在同一个 Web 应用程序中同时使用集成的身份验证/授权和 OWIN Azure AD 身份验证中间件时遇到了类似的问题。
  • 嗨@Phillippe。是的,我确实在门户的 Web 应用程序中启用了该身份验证。这不是必须的吗?
  • 对不起,@ChrisGillum。你发表了那条评论。
  • @ChrisGillum。刚刚有一个灯泡时刻。你是对的。我确实启用了不正确的 Azure Auth 设置!该身份验证是问题所在。我认为这个 UseWindowsAzureActiveDirectoryBearerAuthentication 就是所需要的
  • 你要调用什么api?该错误表明您的访问令牌无法访问该 api...

标签: c# azure azure-web-app-service jwt azure-active-directory


【解决方案1】:

您是否在门户中启用了身份验证/授权?有几个人在同一个 Web 应用程序中同时使用集成的身份验证/授权和 OWIN Azure AD 身份验证中间件时遇到了类似的问题。设置您的应用的正确方法是使用其中一种,因为它们目前不能很好地结合使用。

【讨论】:

  • 删除了 Azure AD Auth 并修复了它。
猜你喜欢
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2018-06-17
  • 1970-01-01
  • 2020-08-12
相关资源
最近更新 更多