[英]How to add NTLM auth to .net core when app could be self hosted or IIS
I have a .net core application that needs to use NTLM authentication. 我有一个.net核心应用程序,需要使用NTLM身份验证。 If I use the package Microsoft.AspNetCore.Server.HttpSys
如果我使用包Microsoft.AspNetCore.Server.HttpSys
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseHttpSys(options =>{
options.Authentication.Schemes = AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
options.Authentication.AllowAnonymous = false;
}).Build()
It will work great if I self host, but if it's deployed on IIS, it will crash. 如果我自行托管,它将很好用,但是如果将其部署在IIS上,则会崩溃。
Same difference if I don't use it and use IIS settings to provide windows auth, if I need to run self hosted it will crash. 如果不使用它,而使用IIS设置提供Windows身份验证,则存在相同的区别,如果我需要运行自托管,它将崩溃。
Is there anyway to make one executable that will work for both scenarios? 无论如何,要使一个可执行文件在两种情况下都适用?
Yes, but this solution is a little shady. 是的,但是这种解决方案有点阴暗。
There is a setting indicating if IIS is providing authentication. 有一个设置指示IIS是否正在提供身份验证。 But that setting is keyed by a constant that is marked internal. 但是该设置由标记为internal的常量键入。 So you can access it, if you hard code the magic string "IIS_HTTPAUTH"
. 因此,如果您对魔术字符串"IIS_HTTPAUTH"
硬编码,则可以访问它。
var webhost = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
//If not hosted by IIS for auth
if (String.IsNullOrEmpty(webhost.GetSetting("IIS_HTTPAUTH"))) {
webhost = webhost.UseHttpSys(options => {
options.Authentication.Schemes = AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
options.Authentication.AllowAnonymous = false;
});
}
return webhost.Build();
Current as of .netcore2.0.X 截至.netcore2.0.X当前
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.