2017-09-04 73 views
2

我有一個非常簡單的MVC5網站,我試圖用IdentityServer3來保護。爲什麼IdentityServer重定向到http而不是https?

我的網站和我的IdentityServer實例都作爲AppHarbor中的獨立站點託管。兩者都支持https。

當我在我的網站中受到[Authorize]屬性(例如,/Home/About)保護的資源時,我已成功重定向到IdentityServer,並且我可以成功進行身份驗證。

當IdentityServer將其響應發回到網站(通過app.FormPostResponse.js)後,網站將302重定向到所請求的資源 - 如預期。但是,此重定向是http,而不是https(請參閱下面的網絡跟蹤)。

我敢肯定,這只是我的IdentityServer配置錯了,但我會很感激任何指針,我有什麼問題。

(AppHarbor使用在IIS中,在SSL終止前的反向代理(nginx的我相信) - 所以我有RequireSsl = false此情況下,如按照IdentityServer文檔)

這裏是我的網站的Startup.cs

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "Cookies" 
     }); 

     app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
     { 
      Authority = "https://<my-idsrv3>.apphb.com/identity", 

      ClientId = "<my-client-id>", 
      Scope = "openid profile roles email", 
      RedirectUri = "https://<my-website>.apphb.com", 
      ResponseType = "id_token", 

      SignInAsAuthenticationType = "Cookies", 

      UseTokenLifetime = false 
     }); 

     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 
    } 
} 

這裏是Startup.cs從我IdentityServer3例如:

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.Map("/identity", idsrvApp => 
     { 
      idsrvApp.UseIdentityServer(new IdentityServerOptions 
      { 
       SiteName = "My Identity Server", 
       SigningCertificate = Certificates.LoadSigningCertificate(), 
       RequireSsl = false, 
       PublicOrigin = "https://<my-idsrv3>.apphb.com", 

       Factory = new IdentityServerServiceFactory() 
        .UseInMemoryUsers(Users.Get()) 
        .UseInMemoryClients(Clients.Get()) 
        .UseInMemoryScopes(Scopes.Get()) 
      }); 
     }); 
    } 
} 

這裏是我的網站的定義客戶:

new Client 
{ 
    Enabled = true, 
    ClientName = "My Website Client", 
    ClientId = "<my-client-id>", 
    Flow = Flows.Implicit, 

    RedirectUris = new List<string> 
    { 
     "https://<my-website>.apphb.com" 
    }, 

    AllowAccessToAllScopes = true 
} 

下面是來自Chrome的痕跡,點擊「是的,允許」的IdentityServer同意屏幕上後:

Chrome network trace

回答

3

所以看起來這個問題是我的客戶的網站引起在SSL終結nginx前端後面。

參考this GitHub issue,添加以下到我的網站的應用程序配置的開始:

app.Use(async (ctx, next) => 
{ 
    string proto = ctx.Request.Headers.Get("X-Forwarded-Proto"); 
    if (!string.IsNullOrEmpty(proto)) 
    { 
     ctx.Request.Scheme = proto; 
    } 
    await next(); 
}); 

這使得網站知道傳入的請求是通過https;這反過來似乎確保IdentityServer3中間件生成https URI。