Azure AD - HTTP/1.1 401 ไม่ได้รับอนุญาต - บริการ Windows

มีปัญหากับ Azure Auth รับ 401 ไม่ได้รับอนุญาตเมื่อเรียก WebAPI ที่โฮสต์ใน Azure

ข้อมูลพื้นฐานต่อไปนี้: ทำตามบทช่วยสอนนี้ - https://azure.microsoft.com/en-us/documentation/samples/active-directory-dotnet-daemon/

ลงทะเบียน 2 ใบสมัคร:

  1. บริการวินโดวส์
  2. เว็บเอพีไอ

บริการ Windows เรียก STS และได้รับ JWT สำเร็จ จากนั้นจะพยายามส่งโทเค็นนั้นไปยัง WebAPI ที่โฮสต์บน azurewebsites.net

นี่คือรหัสของฉันที่จะเรียก STS (เพิ่งรวมอยู่ใน Auth Class):

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 ให้ Unauthorized

ฉันใช้พู้ทำเล่นเพื่อรับคำขอและการตอบกลับ:

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 ได้ ฉันพยายามเพิ่มผ่าน Users แต่เมื่อฉันค้นหา Application นั้นในฐานะผู้ใช้ มันหาไม่พบ แอปพลิเคชันที่ลงทะเบียนแล้วอยู่ในไดเรกทอรี Azure AD อื่น


comment
คุณเปิดใช้งานการรับรองความถูกต้อง / การอนุญาตในพอร์ทัลโดยบังเอิญหรือไม่? หลายคนประสบปัญหาคล้ายกันเมื่อพวกเขาใช้ทั้งมิดเดิลแวร์การรับรองความถูกต้อง / การอนุญาตแบบรวมและมิดเดิลแวร์การรับรองความถูกต้องของ OWIN Azure AD ในเว็บแอปเดียวกัน   -  person Chris Gillum    schedule 24.05.2016
comment
สวัสดี @Phillippe ใช่ ฉันได้เปิดใช้งานการรับรองความถูกต้องนั้นใน Web App ในพอร์ทัล นั่นไม่จำเป็นเหรอ?   -  person Fox    schedule 24.05.2016
comment
ขออภัย @ChrisGillum คุณโพสต์ความคิดเห็นนั้น   -  person Fox    schedule 24.05.2016
comment
@คริสกิลลัม. เพิ่งมีช่วงเวลาหลอดไฟ คุณถูก. ฉันเปิดใช้งานการตั้งค่า Azure Auth ซึ่งไม่ถูกต้อง! การรับรองความถูกต้องนั้นเป็นที่ที่ปัญหาอยู่ ฉันคิดว่า UseWindowsAzureActiveDirectoryBearerAuthentication นี้เป็นสิ่งที่จำเป็นทั้งหมด   -  person Fox    schedule 24.05.2016
comment
คุณต้องการเรียก API อะไร ข้อผิดพลาดระบุว่าโทเค็นการเข้าถึงของคุณไม่สามารถเข้าถึง API นั้นได้   -  person Lily_user4045    schedule 24.05.2016
comment
ขอบคุณ @ Lily_user4045 พบปัญหาจำเป็นต้องทดสอบแต่คริสแก้ไขได้ด้วยคำตอบของเขา   -  person Fox    schedule 24.05.2016
comment
@คริสกิลลัม. หากคุณสามารถโพสต์ของคุณเป็นคำตอบได้ ฉันจะทำเครื่องหมายว่าเป็นคำตอบ :-)   -  person Fox    schedule 24.05.2016
comment
@ฟ็อกซ์ เยี่ยมมาก! ฉันโพสต์ความคิดเห็นของฉันเป็นคำตอบ :)   -  person Chris Gillum    schedule 24.05.2016


คำตอบ (1)


คุณเปิดใช้งานการรับรองความถูกต้อง / การอนุญาตในพอร์ทัลโดยบังเอิญหรือไม่? หลายคนประสบปัญหาคล้ายกันเมื่อพวกเขาใช้ทั้งมิดเดิลแวร์การรับรองความถูกต้อง / การอนุญาตแบบรวมและมิดเดิลแวร์การรับรองความถูกต้องของ OWIN Azure AD ในเว็บแอปเดียวกัน วิธีที่ถูกต้องในการตั้งค่าแอปของคุณคือการใช้อย่างใดอย่างหนึ่งเนื่องจากขณะนี้แอปเหล่านี้เล่นได้ไม่ดีนัก

person Chris Gillum    schedule 24.05.2016
comment
ลบ Azure AD Auth และทำการแก้ไขแล้ว - person Fox; 24.05.2016