且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

的ASP.NET Web API自主机使用Windows身份验证

更新时间:2023-02-17 10:38:58

我已经打了这个问题,以及和我已经想出了唯一的解决办法是提供专门的HttpSelfHostedConfiguration:

I've hit this issue as well and the only solution I've came up with is to deliver dedicated HttpSelfHostedConfiguration:

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 = HttpClientCredentialType.Ntlm;
        return base.OnConfigureBinding(httpBinding);
    }
}

要使用它,你只需要改变一行(你不需要设置UseWindowsAuthentication了):

To use it you just need to change one line (you don't need to set UseWindowsAuthentication anymore):

var config = new NtlmSelfHostConfiguration("http://myComputerName:8080");

这种方法唯一的问题是现在需要对服务器的每个请求是使用此配置的身份验证。

The only issue with this approach is that authentication is now required for every request made to server which is using this configuration.