2017-09-29 3 views
1

IdentityServer3를 로컬로 설치했는데 모든 것이 잘 작동합니다. JWT를 사용하여 사용자를 인증하고 authorize 속성을 사용하여 웹 API 컨트롤러에 성공적으로 액세스 할 수있었습니다.azure에서 IdentityServer3를 설치하십시오.

azure에 업로드 할 때 액세스 토큰을 얻을 수 있지만 컨트롤러에 액세스하려고 시도 할 때 401 오류가 발생합니다. 인증서와 관련 있다고 가정합니다. 내 구성은 다음과 같습니다

public static class Config 
{ 

    /// <summary> 
    /// Configures identity server 
    /// </summary> 
    public static void ConfigureIdentityServer(this IAppBuilder app, CormarConfig config) 
    { 

     // Create our options 
     var identityServerOptions = new IdentityServerOptions 
     { 
      SiteName = "Cormar API", 
      SigningCertificate = LoadCertificate(), 
      IssuerUri = "https://localhost:44313", 

      // Not needed 
      LoggingOptions = new LoggingOptions 
      { 
       EnableHttpLogging = true, 
       EnableWebApiDiagnostics = true, 
       EnableKatanaLogging = true, 
       WebApiDiagnosticsIsVerbose = true 
      }, 

      // In membory crap just to get going 
      Factory = new IdentityServerServiceFactory().Configure(config),   

      // Disable when live 
      EnableWelcomePage = true 
     }; 

     // Setup our auth path 
     app.Map("/identity", idsrvApp => 
     { 
      idsrvApp.UseIdentityServer(identityServerOptions); 
     }); 
    } 


    /// <summary> 
    /// Configures the identity server to use token authentication 
    /// </summary> 
    public static void ConfigureIdentityServerTokenAuthentication(this IAppBuilder app, HttpConfiguration config) 
    { 
     app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
     { 
      Authority = "https://localhost:44313/identity", 
      ValidationMode = ValidationMode.ValidationEndpoint, 
      RequiredScopes = new[] { "api" } 
     }); 

     AntiForgeryConfig.UniqueClaimTypeIdentifier = IdentityServer3.Core.Constants.ClaimTypes.Subject; 
     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 
    } 

    /// <summary> 
    /// Loads the certificate 
    /// </summary> 
    /// <returns></returns> 
    private static X509Certificate2 LoadCertificate() 
    { 
     var certPath = $"{ AppDomain.CurrentDomain.BaseDirectory }App_Data\\idsrv3test.pfx"; 
     return new X509Certificate2(certPath, "idsrv3test"); 
    } 

    /// <summary> 
    /// Configure the identity service factory with custom services 
    /// </summary> 
    /// <returns></returns> 
    private static IdentityServerServiceFactory Configure(this IdentityServerServiceFactory factory, CormarConfig config) 
    { 
     var serviceOptions = new EntityFrameworkServiceOptions { ConnectionString = config.SqlConnectionString }; 
     factory.RegisterOperationalServices(serviceOptions); 
     factory.RegisterConfigurationServices(serviceOptions); 

     factory.CorsPolicyService = new Registration<ICorsPolicyService>(new DefaultCorsPolicyService { AllowAll = true }); // Allow all domains to access authentication 
     factory.Register(new Registration<DbContext>(dr => dr.ResolveFromAutofacOwinLifetimeScope<DbContext>())); 
     factory.UserService = new Registration<IUserService>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IUserService>()); 
     factory.ClientStore = new Registration<IClientStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IClientStore>()); 
     factory.ScopeStore = new Registration<IScopeStore>(dr => dr.ResolveFromAutofacOwinLifetimeScope<IScopeStore>()); 

     return factory; 
    } 
} 

내가 읽고있는 내가 참조 토큰을 사용하는 경우, 내가 그들을 서명 인증서를 사용할 필요가 없습니다 것 같습니다. 그래서 난 내 클라이언트 AccessTokenType 토큰을 참조하도록 변경하고 API를 범위에 비밀을 추가하고 나는 로컬 내 보호 컨트롤러에 액세스 할 수 있었다, 그러나 나는 푸른로 누르면 다시, 난 여전히 401

를 얻을 수 아무도 내가이 문제를 해결할 수있는 방법을 알고 있습니까?

+0

azure에서 실행중인 경우 로컬 호스트와 포트 44313을 사용하는 이유는 무엇입니까? Azure 방화벽에서 열리는이 포트입니까? (사이트 구성)? – rmjoia

+0

언제든지 [Dominick Baier] (https://twitter.com/leastprivilege) – rmjoia

+0

@rmjoia를 트윗 할 수 있습니다. IssuerUri는 언제든지 변경할 수 있습니다. 테스트 할 때 localhost 일 뿐이므로 문제가 발생하지 않습니다. – r3plica

답변

1

UseIdentityServerBearerTokenAuthentication의 설정을 변경하는 것이 해결책이었습니다. 옵션을 다음과 같이 업데이트했습니다 :

DelayLoadMetadata = true, 

그리고 모두 작동하기 시작했습니다.