我使用的是IdentityServer4 + asp.net核心API。在客户端,我使用angular-cli。
在ConfigServices中
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// Enable CORS.
CorsPolicyBuilder corsBuilder = new CorsPolicyBuilder()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowCredentials();
services.AddCors(opts =>
{
opts.AddPolicy("AllowAllOrigins", corsBuilder.Build());
});
services.UseSimpleInjectorAspNetRequestScoping(container);
// identityserver4 configuration
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryClients(Clients.Get())
.AddInMemoryIdentityResources(Resources.GetIdentityResources())
.AddInMemoryApiResources(Resources.GetApiResources())
.AddTestUsers(Users.Get());
// Zip response
services.Configure<GzipCompressionProviderOptions>(opts => opts.Level = CompressionLevel.Optimal);
services.AddResponseCompression(opts =>
{
opts.EnableForHttps = false;
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[]
{
"image/svg+xml",
"application/atom+xml"
});
opts.Providers.Add<GzipCompressionProvider>();
});
// Adds services required for using options.
services.AddOptions();
// Register the IConfiguration instance which MyOptions binds against.
services.Configure<ConfigModel>(Configuration);
}
这是配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions<ConfigModel> optionsAccessor)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIdentityServer();
app.UseMiddleware(typeof(ErrorHandlingMiddleware));
var url = optionsAccessor.Value.SystemConfig.Authority;
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = url,
RequireHttpsMetadata = false,
ApiName = Definitions.WebApiScope/*,
AllowedScopes = { Definitions.WebApiScope }*/
});
app.UseCors("AllowAllOrigins");
app.UseResponseCompression();
app.UseMvcWithDefaultRoute();
// register service
//DependencyRegistrator.RegisterServices(app, container);
//container.Verify();
app.UseMvc();
}
我可以使用令牌端点来检索access_token,并将其用于请求userinfo端点。这是标题:
authJsonHeaders() {
const header = new Headers();
header.append('Content-Type', 'application/json');
header.append('Accept', 'application/json');
header.append('Authorization', 'Bearer ' + localStorage.getItem('id_token'));
return header;
}
但我总是收到错误Response for preflight has invalid HTTP status code 405
我失踪了什么? 提前致谢