2013-01-14 47 views
4

我有以下配置:的ASP.NET Web API - NTLM身份驗證和HTTPS

  1. 自託管的ASP.NET Web API
  2. ASP.NET MVC 3 Web應用程序

網站app [2]通過HTTPS與Web API [1]交流。 他們兩個(現在)住在同一臺機器上。

HTTP訪問的Web API結合[1],配置這樣的:

httpBinding.Security.Mode = HttpBindingSecurityMode.Transport; httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
httpBinding.TransferMode = TransferMode.Streamed

我不能使它工作使用HTTPS和NTLM授權。

  • 如果我通過純HTTP進行通信它的工作原理,我正確地驗證
  • 如果我溝通通過HTTPS它給了我「401未經授權」的錯誤有哪些[授權]標籤(它適用於所有的控制器動作不需要授權的操作)

爲什麼只改變傳輸協議(從http到https)會阻止NTLM身份驗證正常工作?

感謝您的任何幫助!

+0

一件事澄清 - HTTPS是Web應用程序和Web API之間使用,而Web應用程序和瀏覽器之間 –

+0

你確信[授權]是System.Web.Http.Authorize? –

+0

是的,System.Web.Http.AuthorizeAttribute。當使用http傳輸時,它可以正常工作。只有將其更改爲https才能打破授權... –

回答

2

@Jacek Nowak 我自己和今天遇到了同樣的問題,我只是遇到了以下post中詳述的答案。

下面是我將如何編寫它。

public class NTLMSelfHostConfiguration : HttpSelfHostConfiguration 
{ 
    public NTLMSelfHostConfiguration(string baseAddress) : base(baseAddress) { } 
    public NTLMSelfHostConfiguration(Uri baseAddress) : base(baseAddress) { } 
    protected override BindingParameterCollection OnConfigureBinding(HttpBinding httpBinding) 
    { 
     httpBinding.Security.Mode = HttpBindingSecurityMode.TransportCredentialOnly; 
     httpBinding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Ntlm; 
     httpBinding.ConfigureTransportBindingElement = 
      element => element.AuthenticationScheme = 
       System.Net.AuthenticationSchemes.IntegratedWindowsAuthentication; 
     return base.OnConfigureBinding(httpBinding); 
    } 
} 


public static class Program() 
{ 
    public static void main(string[] args) 
    { 
     var config = new NTLMSelfHostConfiguration("https://localhost/");    
     config.Routes.MapHttpRoute("Main", 
            "api/{controller}"); 

     var server = new HttpSelfHostServer(config); 

     server.OpenAsync().Wait(); 

     Console.WriteLine("Running"); 
     Console.ReadLine(); 

     server.CloseAsync().Wait(); 


    } 
}