【发布时间】:2017-06-09 05:30:29
【问题描述】:
我按照this 文章在我们的项目中实现了条件中间件,它工作得非常好。 但是当我们将项目从 .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
});
});
//FOR API
app.UseWhen(context => isApiRequest(context), appBuilder =>
{
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions()
{
Authority = authority,
RequireHttpsMetadata = false,
AllowedScopes =
{
"scope1",
"scope2"
}
});
});
现在,当我尝试访问基于 View 的 ActionMethod(意味着不从 api 开始)时,即使在这种情况下 api 身份验证工作,而不是基于 Cookie 的身份验证。基于 API 的身份验证工作得非常好。
我们正在最新版本的 Identity Server 4 上构建我们的项目。
任何帮助/指针将不胜感激。
【问题讨论】:
-
您不应该在条件分支中使用
appBuilder而不是app吗?只是一个想法,它看起来很奇怪。 -
真的很奇怪。由于配置函数在启动时运行,它将注册所有这些中间件,就好像条件从未存在一样。
-
只是想补充一下,我在 VS2017 .net core 1.1 中遇到了同样的问题。给定的委托似乎从未被执行。
标签: c# asp.net asp.net-core .net-core identityserver4