首页 > 解决方案 > IdentityServer4 提供未格式化的支持范围

问题描述

问题是我有一个名为 tv-participants-lookup 的 .net core 3.1 和 reactjs 项目,在实施和配置 serveridentity4 之后,当尝试注册或登录时,它返回 Invalid Scope 错误,因此问题如下所示.well-known/openid 配置:

{
    "issuer": "https://localhost:5001",
    "jwks_uri": "https://localhost:5001/.well-known/openid-configuration/jwks",
    "authorization_endpoint": "https://localhost:5001/connect/authorize",
    "token_endpoint": "https://localhost:5001/connect/token",
    "userinfo_endpoint": "https://localhost:5001/connect/userinfo",
    "end_session_endpoint": "https://localhost:5001/connect/endsession",
    "check_session_iframe": "https://localhost:5001/connect/checksession",
    "revocation_endpoint": "https://localhost:5001/connect/revocation",
    "introspection_endpoint": "https://localhost:5001/connect/introspect",
    "device_authorization_endpoint": "https://localhost:5001/connect/deviceauthorization",
    "frontchannel_logout_supported": true,
    "frontchannel_logout_session_supported": true,
    "backchannel_logout_supported": true,
    "backchannel_logout_session_supported": true,
    "scopes_supported": [
        "openid",
        "profile",
        "API.ClientId",
        "TV Participants lookupAPI", **<=======** you may notice the space between the words causing it to appear like three scopes when validating the scopes
        "offline_access"
    ],

这是我的 appsettings.json:

"IdentityServer": {
    "Clients": {
      "client1": {
        "Profile": "IdentityServerSPA",
        "AlowedScopes":"openid profile"
      
      }
    },
    "Resources": {
      "API.ClientId": {
        "Profile": "API"
      }
    } 
},

startup.cs 如下:

 public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
         services.AddDbContext<ApplicationDbContext>(options =>                      options.UseSqlServer(Configuration.GetConnectionString(("DefaultConnection"))));
        services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
         .AddRoles<IdentityRole>()
        .AddRoleManager<RoleManager<IdentityRole>>()     
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddIdentityServer()
            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();          
        services.AddAuthorization();
           services.Configure<IdentityOptions>(options =>
    {
        options.ClaimsIdentity.RoleClaimType = JwtClaimTypes.Role;
    });
        services.AddAuthentication()
            .AddIdentityServerJwt();
        services.AddTransient<IProfileService, ProfileService>();
        services.AddControllersWithViews();
        services.AddRazorPages();
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        app.UseIdentityServer();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";
            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }

所以问题是 openid-connect 从哪里得到这个范围 - “TV Participants lookupAPI” - 从?有没有办法删除或更改 scopes_supported ?

标签: reactjs.net-coreidentityserver4openid-connectscopes

解决方案


想通了,因为我必须更改项目名称并删除空格。一旦.csproj命名TV_Participants_lookupAPI,问题就解决了。


推荐阅读