gpt4 book ai didi

c# - 无法加载 http ://localhost:5000/. 众所周知/openid-配置:请求的资源上不存在 'Access-Control-Allow-Origin' header

转载 作者:太空狗 更新时间:2023-10-29 23:49:15 26 4
gpt4 key购买 nike

我是identityserver4的新手,最近看到identityserver团队提供的Quickstart8示例,里面包含3个项目 1.Identityserver2. Api 3.当我部署到 iis 时,客户端在浏览器中都工作正常,它们不能正常工作,它显示错误,如...

enter image description here

我正在使用 javascript 客户端 ...

请帮我解决这个问题。

这是我的代码...

API(启动.cs)

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Api
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;

options.ApiName = "api1";
});

services.AddCors(options =>
{
// this defines a CORS policy called "default"
options.AddPolicy("default", policy =>
{
policy.WithOrigins("http://localhost:5003")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
}

public void Configure(IApplicationBuilder app)
{
app.UseCors("default");

app.UseAuthentication();

app.UseMvc();
}
}

API(身份 Controller )

[Route("[controller]")]
[Authorize]
public class IdentityController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}

QuickstartIdentityServer (startup.cs)

 public class Startup
{


public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

string connectionString = @"Data Source=DOTNET-Foo;Initial Catalog=IdentityServer4;Integrated Security=True";
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddTestUsers(Config.GetUsers())
// this adds the config data from DB (clients, resources)
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder =>
builder.UseSqlServer(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
});
// this adds the operational data from DB (codes, tokens, consents)
//.AddOperationalStore(options =>
//{
// options.ConfigureDbContext = builder =>
// builder.UseSqlServer(connectionString,
// sql => sql.MigrationsAssembly(migrationsAssembly));

// // this enables automatic token cleanup. this is optional.
// options.EnableTokenCleanup = true;
// options.TokenCleanupInterval = 30;
//});

services.AddAuthentication()
.AddGoogle("Google", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

options.ClientId = "434483408261-55tc8n0cs4ff1fe21ea8df2o443v2iuc.apps.googleusercontent.com";
options.ClientSecret = "3gcoTrEDPPJ0ukn_aYYT6PWo";
})
.AddOpenIdConnect("oidc", "OpenID Connect", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.SignOutScheme = IdentityServerConstants.SignoutScheme;

options.Authority = "https://demo.identityserver.io/";
options.ClientId = "implicit";

options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// IdentityServerDatabaseInitialization.InitializeDatabase(app);
}
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
}

我无法访问 http://localhost:5000/.well-known/openid-configuration

enter image description here

最佳答案

我认为当您从 IIS 运行项目时,该示例不再有效,因为地址,或者更准确地说,端口不相同。

在 IIS Express 中运行时使用的端口

当您通过 Visual Studio 或使用 dotnet run 运行项目时,托管项目的 URL 由 launchSettings.json 中的文件驱动 Properties 项目的文件夹。

关联配置

知道这一点后,有一些配置设置开始发挥作用;让我们一起解决它们。

IdentityServer 中的客户端设置

当您定义客户端(即,将其身份验证联合到 IdentityServer 的应用程序)时,您需要指定一些内容,例如:

  • 允许 IdentityServer 在登录或注销后将用户重定向到哪个 URL;
  • 如果这是一个 JS 客户端,应该允许浏览器从哪个 URL 发起授权请求

这可以在 Config 类中找到 over here .

您会注意到,在使用 IIS Express 时,该配置中指定的所有 URL 都指向托管 JavaScriptClient 的位置;当部署到 IIS 时,您需要将它们更新为 JS 客户端的 URL。

JS配置

由于在这个例子中,JS 客户端直接向 IdentityServer 发出请求,所以一些设置是在 JS 应用程序本身中定义的;我们可以在 app.js 中找到它们文件:

  • authority 是 IdentityServer URL - localhost:5000 在我们使用 IIS Express 时是正确的
  • redirect_uripost_logout_redirect_uri 使用localhost:5003,这是我们使用IIS Express 时的JS 客户端URL

同样,当您使用 IIS 时,您需要更新所有这些值以匹配托管两个应用程序的 URL。

API配置

此示例展示了 JS 客户端如何向 API 发出请求并将 token 发送到 IdentityServer 以对其进行验证。

这里涉及到一些设置:

  • JS 客户端需要知道 API 的 URL - 这在 app.js 中再次定义在 JS 客户端中
  • API 需要知道如何访问 IdentityServer - 我们会在 Startup.cs 中找到它API的
  • API 需要通过 CORS 策略允许浏览器向其端点发出 AJAX 请求,这同样在 Startup class 中完成。在 API 项目中

再一次,您需要更新所有这些 URL 以匹配将项目部署到 IIS 时使用的 URL。

希望我没有遗漏任何东西 ;-)

关于c# - 无法加载 http ://localhost:5000/. 众所周知/openid-配置:请求的资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51531832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com