6

Tôi nhận được lỗi dưới đây trong khi gửi yêu cầu đến ứng dụng Máy chủ Nhận dạng của tôi từ Ứng dụng Máy khách Javascript của tôi.Nhận phạm vi Xác nhận lỗi trong Máy chủ Nhận dạng 4 sử dụng Máy khách JavaScript trong lõi asp.net

thất bại: IdentityServer4.Validation.ScopeValidator [0] phạm vi hợp lệ: openid

Tôi đã thực hiện chắc chắn tôi thêm phạm vi trong ứng dụng nhận dạng máy chủ của tôi. Dưới đây là mã của tôi.

IdentityServer Application (Host) Config.cs

public class Config 
{ 
    public static IEnumerable<ApiResource> GetApiResources() 
    { 
     return new List<ApiResource> 
     { 
      new ApiResource("api1","My API") 
     }; 
    } 

    public static IEnumerable<Client> GetClients() 
    { 
     return new List<Client> 
     { 
      new Client 
      { 
       ClientId = "js", 
       ClientName = "javaScript Client", 
       AllowedGrantTypes = GrantTypes.Implicit, 
       AllowAccessTokensViaBrowser = true, 
       RedirectUris = { "http://localhost:5003/callback.html" }, 
       PostLogoutRedirectUris = { "http://localhost:5003/index.html" }, 
       AllowedCorsOrigins = { "http://localhost:5003" }, 
       AllowedScopes = 
        { 
         IdentityServerConstants.StandardScopes.OpenId, 
         IdentityServerConstants.StandardScopes.Profile, 
         "api1" 
        } 
      } 
     }; 
    } 
} 

Startup.cs

public class Startup 
{ 
    // This method gets called by the runtime. Use this method to add services to the container. 
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 
    public void ConfigureServices(IServiceCollection services) 
    { 

     services.AddIdentityServer() 
      .AddTemporarySigningCredential() 
      .AddInMemoryApiResources(Config.GetApiResources()) 
      .AddInMemoryClients(Config.GetClients()); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      app.UseIdentityServer(); 
     } 

     app.Run(async (context) => 
     { 
      await context.Response.WriteAsync("Hello World!"); 
     }); 
    } 
} 

Web API Startup.cs

public class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 

     if (env.IsEnvironment("Development")) 
     { 
      // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. 
      builder.AddApplicationInsightsSettings(developerMode: true); 
     } 

     builder.AddEnvironmentVariables(); 
     Configuration = builder.Build(); 
    } 

    public IConfigurationRoot Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container 
    public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddApplicationInsightsTelemetry(Configuration); 

     services.AddCors(option => 
     { 
      option.AddPolicy("dafault", policy => 
      { 
       policy.WithOrigins("http://localhost:5003") 
         .AllowAnyHeader() 
         .AllowAnyMethod(); 
      }); 
     }); 
     services.AddMvcCore() 
       .AddAuthorization() 
       .AddJsonFormatters(); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 

     //this uses the policy called "default" 
     app.UseCors("default"); 

     app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     { 
      Authority = "http://localhost:5000", 
      AllowedScopes = { "api1" }, 
      RequireHttpsMetadata = false 
     }); 

     app.UseApplicationInsightsRequestTelemetry(); 

     app.UseApplicationInsightsExceptionTelemetry(); 

     app.UseMvc(); 
    } 
} 
+0

im gặp cùng một lỗi trong khi theo cùng một mẫu trong tài liệu. Bạn đã có bất kỳ may mắn? – alessalessio

Trả lời

17

Trong khi khách hàng của bạn (ứng dụng) được cấu hình hay được phép yêu cầu tài nguyên openid (hoặc phạm vi), máy chủ danh tính của bạn không được cấu hình cho tài nguyên sắc openid

Bạn cần phải thêm nó như là một nguồn tài nguyên sắc tương tự như cách thực hiện here và có phương thức trả về tất cả tài nguyên danh tính của bạn mà bạn muốn sử dụng như đã hoàn thành here.

Nói tóm lại thêm một phương pháp mới để Config.cs của bạn trông như thế này:

public static List<IdentityResource> GetIdentityResources() 
{ 
    return new List<IdentityResource> 
    { 
     new IdentityResources.OpenId() 
     new IdentityResources.Profile() // <-- usefull 
    } 
} 

Và sau đó đến identityservers của bạn chứa dịch vụ thêm cấu hình tài nguyên danh tính của bạn như thế này:

services.AddIdentityServer() 
    .AddTemporarySigningCredential() 
    .AddInMemoryApiResources(Config.GetApiResources()) 
    .AddInMemoryClients(Config.GetClients()); 
    .AddInMemoryIdentityResources(Config.GetIdentityResources()) // <-- adding identity resources/scopes 
+0

Làm việc của nó bây giờ, Nhưng bây giờ hiển thị của nó Hiển thị đăng nhập: Người dùng không được xác thực – maxspan

+0

Nhật ký là bạn của bạn, là bàn điều khiển của bạn đăng nhập vào cấp độ dấu vết? – Lutando

+0

có. Nó hiển thị IdentityServer4.ResponseHandling.AuthorizeInteractionResponseGenerator [0] Người dùng không được xác thực. Làm cách nào để tôi xác thực người dùng – maxspan

Các vấn đề liên quan