API ภายในเซิร์ฟเวอร์ข้อมูลประจำตัว 4

ฉันใช้ Identity Server 4 และเพิ่มจุดสิ้นสุด API ที่จะใช้ แต่ดูเหมือนจะไม่สามารถทำให้มันทำงานได้อย่างถูกต้อง เมื่อฉันส่งการเรียกไปยังเซิร์ฟเวอร์ข้อมูลประจำตัว API ด้วยโทเค็นการเข้าถึงระบบจะแจ้งว่า

Microsoft.IdentityModel.Tokens.SecurityTokenInvalidAudienceException: IDX10214: Audience validation failed.

และการอ้างสิทธิ์ชื่อ API ไม่ได้อยู่ในโทเค็นการเข้าถึง ฉันกำลังใช้ที่เก็บการกำหนดค่า DB

นี่คือวิธีที่ฉันกำหนดค่าเซิร์ฟเวอร์ข้อมูลประจำตัว 4

Startup.cs

    services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>(options=>
                {
                    // example of setting options
                    options.Tokens.ChangePhoneNumberTokenProvider = "Phone";

                    // password settings chosen due to NIST SP 800-63
                    options.Password.RequiredLength = 3; // personally i'd prefer to see 10+
                    options.Password.RequiredUniqueChars = 0;
                    options.Password.RequireDigit = false;
                    options.Password.RequireLowercase = false;
                    options.Password.RequireUppercase = false;
                    options.Password.RequireNonAlphanumeric = false;
                })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            // Add application services.
            services.AddTransient<IEmailSender, EmailSender>();

            services.AddMvc();

            var migrationsAssembly = typeof(StartupDevelopment).GetTypeInfo().Assembly.GetName().Name;
            // configure identity server with in-memory stores, keys, clients and scopes
            services.AddIdentityServer(options =>
                    {
                        options.Discovery.CustomEntries.Add("Claims", "/api/claims");                         
                    }
                )                
                .AddDeveloperSigningCredential()
                .AddDeveloperSigningCredential()             
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(Configuration.GetConnectionString("ConfigurationStore"),
                            sql => sql.MigrationsAssembly(migrationsAssembly));
                })                
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(Configuration.GetConnectionString("OperationalStore"),
                            sql => sql.MigrationsAssembly(migrationsAssembly));

                    // this enables automatic token cleanup. this is optional.
//                    options.EnableTokenCleanup = true;
//                    options.TokenCleanupInterval = 30;
                })               
                .AddAspNetIdentity<ApplicationUser>();

            services.AddAuthentication()
                .AddIdentityServerAuthentication("token", isAuth =>
                {
                    isAuth.Authority = IdentityServerConfigurations.Authority;
                    isAuth.ApiName = "claims"; // TODO change this name to refelect broader api changes
                    isAuth.RequireHttpsMetadata = IdentityServerConfigurations.Ssl;
                });

Config.cs

new Client
                {
                    ClientId = "mvc",
                    ClientName = "MVC Client",
                    AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

                    RequireConsent = false,

                    AccessTokenLifetime = 300,                           
                    UpdateAccessTokenClaimsOnRefresh = true,                                                                    
                    ClientSecrets = 
                    {
                        new Secret("secret".Sha256())
                    },

                    RedirectUris = { "https://localhost:44383/signin-oidc" },
                    PostLogoutRedirectUris = { "https://localhost:44383/signout-callback-oidc" },


                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        IdentityServerConstants.StandardScopes.Address,
                        "claims"                               
                    },
                    AllowOfflineAccess = true
                }

ใน ตัวควบคุมการเรียกร้อง

[Authorize(AuthenticationSchemes = "token")] 

person Epistemologist    schedule 06.03.2018    source แหล่งที่มา
comment
นั่นคือคลาสเริ่มต้นสำหรับ IdentityServer ของคุณหรือ API ที่ได้รับการป้องกันภายใต้ IdentityServer   -  person aaronR    schedule 06.03.2018
comment
คุณต้องการให้ไคลเอ็นต์ MVC สามารถใช้ Claims API ได้หรือไม่   -  person aaronR    schedule 06.03.2018
comment
ทำไมคุณทำเช่นนี้?   -  person aaronR    schedule 07.03.2018
comment
.AddIdentityServerAuthentication(“โทเค็น” ?   -  person aaronR    schedule 07.03.2018
comment
@aaronR ฉันกำลังพยายามเพิ่มคะแนนพิเศษในเซิร์ฟเวอร์ Identity เพื่อให้ฉันสามารถใช้ฟังก์ชันอื่นได้ การเริ่มต้นนี้คือคลาสเริ่มต้น IdentityServer 4   -  person Epistemologist    schedule 07.03.2018
comment
คลาสเริ่มต้นที่คุณมีคือสิ่งที่คล้ายกับที่ API จะมีมากกว่า IdentityServer4   -  person aaronR    schedule 07.03.2018
comment
การเริ่มต้นนั้นเหมือนกับการเพิ่มจุดสิ้นสุด API อื่นๆ ให้กับการใช้งาน IdentityServer4 ของคุณ โดยที่ IdentityServer4 ของคุณจะโฮสต์ API การอ้างสิทธิ์ของคุณด้วย   -  person aaronR    schedule 07.03.2018
comment
docs.identityserver.io/en/release/topics/add_apis.html   -  person Epistemologist    schedule 07.03.2018
comment
ชั้นเรียนเริ่มต้นที่เหลือของคุณอยู่ที่ไหน?   -  person aaronR    schedule 07.03.2018
comment
โปรดดูคำถามที่อัปเดตสำหรับรหัสคลาสเริ่มต้นแบบเต็ม   -  person Epistemologist    schedule 07.03.2018
comment
ให้เราสนทนาต่อในแชท   -  person Epistemologist    schedule 07.03.2018
comment
ลองลบรายการที่สองเพื่อเพิ่มใบรับรองนักพัฒนา   -  person aaronR    schedule 07.03.2018
comment
ยังคงปัญหาเดียวกัน IDX10214: การตรวจสอบผู้ชมล้มเหลว   -  person Epistemologist    schedule 07.03.2018


คำตอบ (2)


ตกลง ฉันขาดการกำหนดค่าเพื่อขอขอบเขตจากฝั่งไคลเอ็นต์

options.Scope.Add("การอ้างสิทธิ์");

services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies")                
                .AddOpenIdConnect("oidc", options =>
                {
                    options.Scope.Add("claims");
                };
person Epistemologist    schedule 07.03.2018

ไม่เห็นการใช้งานการตรวจสอบโทเค็นที่นี่ ซึ่งเป็นสิ่งที่ใช้สำหรับ "โทเค็น"

คุณต้องใช้ Token Validation Handler หากคุณต้องการให้สามารถใช้โทเค็นเพื่อเข้าถึง API ที่กำหนดเอง (ด้วย "โทเค็น")

โปรดดูเอกสารตัวอย่างเกี่ยวกับวิธีการดำเนินการนี้:

http://docs.identityserver.io/en/release/topics/apis.html#refprotectingapis

person Aeseir    schedule 06.03.2018
comment
ฉันดำเนินการโดยใช้เอกสารประกอบ docs.identityserver.io/en/release/topics/add_apis.html - person Epistemologist; 07.03.2018
comment
เยี่ยมมาก คุณได้ใช้การตรวจสอบโทเค็นด้วยหรือไม่ เพราะนั่นเป็นข้อกำหนดเบื้องต้น - person Aeseir; 07.03.2018
comment
การใช้โทเค็นอะไร ฉันคิดว่า services.AddAuthentication() .AddIdentityServerAuthentication(token, isAuth =› { isAuth.Authority = localhost:44387; isAuth. ApiName = การอ้างสิทธิ์; isAuth.RequireHttpsMetadata = true; }); ปกปิดมัน - person Epistemologist; 07.03.2018
comment
รหัสนี้ควรจะดูแลการตรวจสอบความถูกต้องของโทเค็น แต่ฉันติดอยู่กับการแทรกผู้ชมในโทเค็นการเข้าถึง - person Epistemologist; 07.03.2018