aspnet-core 中的条件中间件在 asp.net core 1.1 中不起作用

War*_*rog 3 c# asp.net .net-core asp.net-core identityserver4

我按照这篇文章在我们的项目中实现了条件中​​间件,它运行得很好。但当我们将项目从 .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"
        }
    });
});                      
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试访问基于视图的 ActionMethod(意味着不从 api 开始)时,即使在这种情况下,api 身份验证也能工作,而不是基于 Cookie 的身份验证。基于 API 的身份验证工作得非常好。

我们正在最新版本的 Identity Server 4 上构建我们的项目。

任何帮助/指示将不胜感激。

juu*_*nas 5

将两个条件块更改为使用appBuildernot app

博客文章有误。您现在将它们注册到顶级中间件堆栈而不是子构建器上。

例如:

app.UseWhen(context => !isApiRequest(context), appBuilder =>
        {
            appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationScheme = "Cookies",
                AutomaticAuthenticate = true
            });

            appBuilder.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)