【问题标题】:conditional-middleware-in-aspnet-core is not working in asp.net core 1.1条件中间件在 aspnet 核心中不工作在 asp.net 核心 1.1
【发布时间】: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


【解决方案1】:

将两个条件块更改为使用appBuilder 而不是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
            });
        });

【讨论】:

  • 你的意思是它不工作?尽管不应该触发某些中间件,但是否仍会触发?添加断点,看看发生了什么。
  • 当我通过单击视图执行我的操作方法时,断点确实在委托中命中,说操作方法不是以 api 开头,但我无法进一步调试它。并且代码确实检查了“UseWhen”条件。
  • 日志。 idsrv 未通过身份验证。请求路径 /Home/PlayerLogin 与支持的文件类型不匹配 用户授权失败:(null)。过滤器“Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter”AuthenticationScheme 处的请求授权失败:Cookie 未通过身份验证
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
相关资源
最近更新 更多