我按照这篇文章在我们的项目中实现了条件中间件,它运行得很好。但当我们将项目从 .netcore 1.0 升级到 .netcore 1.1 时,它不起作用。
我在我的启动中编写了以下代码。
Func<HttpContext, bool> isApiRequest = (HttpContext context) => context.Request.Path.ToString().StartsWith("/api/");
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
//For MVC not API
app.UseWhen(context => !isApiRequest(context), appBuilder =>
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies",
AutomaticAuthenticate = true
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
AutomaticChallenge = true,
Authority = authority,
RequireHttpsMetadata = false,
ClientId = "sampleClient",
//ClientSecret = "secret",
Scope = { "openid" , "profile" },
ResponseType = "id_token token",//"code id_token",
SaveTokens = true
}); …Run Code Online (Sandbox Code Playgroud)