我曾尝试在搜索文档和其他地方搜索示例代码,但没有运气
WebAPI启动
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options => {
options.Authority = "https://localhost:44398/";
options.ApiName = "customAPI"; // required audience of access tokens
options.RequireHttpsMetadata = false; // dev only!
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseMvc();
}
API注册
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.Implicit,
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"customAPI.read",
"customAPI.write"
},
// where to redirect to after login
RedirectUris = { "https://localhost:44356/signin-oidc" },
// where to redirect to after logout
PostLogoutRedirectUris = { "https://localhost:44356/signout-callback-oidc" },
RequireConsent = false,
}