การรับรองความถูกต้อง netcore2 กับ Azure Active Directory: ลายเซ็นไม่ถูกต้อง

ฉันมี webapi และต้องมีการตรวจสอบสิทธิ์ ฉันกำลังทำสิ่งนี้กับ AAD ใน V1

ฉันเตรียมทุกอย่างไว้แล้ว สำหรับบุรุษไปรษณีย์ฉันสามารถได้รับโทเค็น แต่เมื่อใดก็ตามที่ฉันพยายามส่งคำขอไปยัง API มันทำให้ฉันมีข้อผิดพลาด 401:

ผู้ถือข้อผิดพลาด = "invalid_token", error_description = "ลายเซ็นไม่ถูกต้อง"

นี่คือรหัสใน ConfigurationServices:

    services.AddAuthentication(sharedOptions =>
    {
        sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

    })
    .AddAzureAdBearer(options => Configuration.Bind("AzureAd", options));

นี่คือคลาสที่สร้างขึ้นสำหรับการรับรองความถูกต้องของ Azure:

    public static AuthenticationBuilder AddAzureAdBearer(this AuthenticationBuilder builder)
        => builder.AddAzureAdBearer(_ => { });

    public static AuthenticationBuilder AddAzureAdBearer(this AuthenticationBuilder builder, Action<AzureAdOptions> configureOptions)
    {
        builder.Services.Configure(configureOptions);
        builder.Services.AddSingleton<IConfigureOptions<JwtBearerOptions>, ConfigureAzureOptions>();
        builder.AddJwtBearer();
        return builder;
    }

    private class ConfigureAzureOptions: IConfigureNamedOptions<JwtBearerOptions>
    {
        private readonly AzureAdOptions _azureOptions;

        public ConfigureAzureOptions(IOptions<AzureAdOptions> azureOptions)
        {
            _azureOptions = azureOptions.Value;
        }

        public void Configure(string name, JwtBearerOptions options)
        {
            options.Audience = _azureOptions.ClientId;
            options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
        }

        public void Configure(JwtBearerOptions options)
        {
            Configure(Options.DefaultName, options);
        }
    }

มีอะไรหายไปที่นี่? โปรดช่วยได้ไหม?

ขอบคุณ


comment
คุณจะส่งโทเค็นผู้ถือได้อย่างไร? ควรมีส่วนหัว Authorization Bearer <your token goes here> ดูที่คำตอบนี้เพื่อดูว่าจะช่วยคุณในการกำหนดค่า API ได้หรือไม่   -  person pcdev    schedule 27.09.2017
comment
ใช่แล้ว ฉันมีหัวหน้า...   -  person HLourenco    schedule 27.09.2017
comment
@HLourenco คุณจำได้ไหมว่าคุณเคยแก้ไขปัญหานี้หรือไม่? ถ้าเป็นเช่นนั้นอย่างไร? ฉันมีปัญหาเดียวกัน ...   -  person Gabriel Bourgault    schedule 26.07.2018
comment
@Gabriel Bourgault ฉันได้แก้ไขปัญหานี้แล้ว ฉันคิดว่าสิ่งที่ฉันทำคือใน AAD ฉันเปลี่ยนการอนุญาตของแอปของฉัน และจากจุดนั้นเป็นต้นไป โทเค็นนั้นถูกต้องเนื่องจากเอนทิตีได้รับการยอมรับ   -  person HLourenco    schedule 27.07.2018


คำตอบ (2)


ดังที่ pcdev พูดในความคิดเห็นของเขา ตรวจสอบให้แน่ใจว่าคุณได้เพิ่มส่วนหัวการอนุญาตให้กับคำขอทั้งหมด

โทเค็น JWT ควรแบ่งออกเป็นสามส่วนคั่นด้วยจุด ลายเซ็นเป็นส่วนที่สาม

คุณสามารถตรวจสอบโทเค็นที่คุณส่งได้ที่ http://jwt.calebb.net หรือ https://jwt.io

person RasmusW    schedule 27.09.2017
comment
มันใช้งานไม่ได้ ฉันมีส่วนหัวที่ถูกต้อง แต่มันก็ใช้งานไม่ได้เช่นกัน ลายเซ็นมี [ลายเซ็น] ในตอนท้าย มีอะไรอีกบ้างในนั้น? - person HLourenco; 27.09.2017

ฉันมีปัญหาที่คล้ายกันและเพิ่งใช้สิ่งต่อไปนี้และใช้งานได้

services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
            .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
person colin powell    schedule 20.03.2019