Asp.net mvc 3 ASP.NET Web API-NTLM身份验证和HTTPS

Asp.net mvc 3 ASP.NET Web API-NTLM身份验证和HTTPS,asp.net-mvc-3,https,asp.net-web-api,ntlm,Asp.net Mvc 3,Https,Asp.net Web Api,Ntlm,我有以下配置: 自托管ASP.NET Web API ASP.NETMVC3Web应用程序 Web应用程序[2]通过HTTPS与Web API[1]通信。 他们俩(目前)生活在同一台机器上 Web API[1]的Http绑定配置如下: httpBinding.Security.Mode=HttpBindingSecurityMode.Transport httpBinding.Security.Transport.ClientCredentialType=HttpClientCredential

我有以下配置:

  • 自托管ASP.NET Web API
  • ASP.NETMVC3Web应用程序
  • Web应用程序[2]通过HTTPS与Web API[1]通信。 他们俩(目前)生活在同一台机器上

    Web API[1]的Http绑定配置如下:

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

    我无法使用https和ntlm授权使其正常工作

    • 如果我通过普通http进行通信,它就会工作,并且我已经正确地进行了身份验证
    • 如果我通过https进行通信,则所有带有[Authorize]标记的控制器操作都会出现“401 Unauthorized”(401 Unauthorized)错误(它适用于不需要授权的操作)
    为什么仅更改传输协议(从http更改为https)会停止NTLM身份验证的工作

    谢谢你的帮助

    @Jacek Nowak 我自己也遇到过同样的问题,今天我刚刚找到了答案,详细内容如下

    下面是我将如何编写代码

    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();
    
    
        }
    }
    

    需要澄清的是,HTTPS是在web应用程序和web api之间使用的,而不是在web应用程序和浏览器之间使用的。您确定[Authorize]是System.web.Http.Authorize吗?是的,System.web.Http.AuthorizeAttribute。当http用于传输时,它可以正常工作。仅将此更改为https会破坏授权。。。