2017-01-24 3 views
2

우리 프로젝트에서 조건부 미들웨어를 구현하는 this 문서를 따라하고 완벽하게 잘 작동했다. 하지만 우리는 .netcore 1.0에서 .netcore 1.1로 프로젝트를 업그레이드 했으므로 작동하지 않습니다.조건부 - 미들웨어 - in - aspnet 코어 asp.net 코어 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" 
     } 
    }); 
});      

는 지금은 기반 인증을 쿠키하지, 심지어 조건 API를 인증 작업에보기를 기반으로 ActionMethod를 (API로 시작하지 의미)에 액세스하려고 할 때. 그리고 API 기반 인증은 완벽하게 작동합니다.

우리는 신원 서버 4.

어떤 도움의 최신 버전에 우리의 프로젝트를 구축하고는/포인터는 이해할 수있을 것이다.

+0

당신은 사용하지 않는해야'appBuilder' 대신'app'의 조건 분기 안에? 그냥 생각, 그것은 이상하게 보입니다. – juunas

+0

정말 이상합니다. config 함수는 시작할 때 실행되기 때문에 ** 그것은 조건이 존재하지 않는 것처럼 모든 미들웨어를 등록합니다 **. – juunas

+0

그냥 VS2017 .net core 1.1에서 같은 문제가 발생했습니다. 주어진 대의원은 결코 처형 된 것처럼 보이지 않습니다. – NoSaidTheCompiler

답변

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 
      }); 
     }); 
+0

아니요, appBuilder를 사용하여 작동하지 않습니다. –

+0

그것이 작동하지 않는다는 것이 무엇을 의미합니까? 일부 미들웨어가 실행되지 않아도 여전히 트리거됩니까? 중단 점을 추가하고 무슨 일이 일어나는지보십시오. – juunas

+0

보기에서 클릭하여 내 동작 메서드를 실행할 때 중단 점은 대리자에서 해당 동작 메서드가 api로 시작하지 않고 더 이상 디버깅 할 수 없다고 말하면서 충돌합니다. 그리고 코드는 "UseWhen"조건을 모두 체크합니다. –