ASP.NET Web API - NTLM身份验证和HTTPS

4
我有以下配置:
  1. 自托管的ASP.NET Web API
  2. ASP.NET MVC 3 Web应用程序
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未经授权”的错误(但不需要授权的操作可以正常运行)。
为什么仅更改传输协议(从http到https)会停止NTLM身份验证的工作?
感谢您提供的任何帮助!

只有一件事需要澄清 - HTTPS 是在 Web 应用程序和 Web API 之间使用的,而不是在 Web 应用程序和浏览器之间使用的。 - Jacek Nowak
你确定 [Authorize] 是 System.Web.Http.Authorize 吗? - Filip W
是的,System.Web.Http.AuthorizeAttribute。当使用http传输时,它可以正常工作。只有将其更改为https才会破坏授权... - Jacek Nowak
1个回答

2

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


    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接