简体   繁体   English

当应用程序可以自托管或IIS时如何将NTLM身份验证添加到.net核心

[英]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.

相关问题 asp.net核心自托管应用在Webhost重生时泄漏 - asp.net core self hosted app leaks on webhost respawn IIS托管的.net核心应用返回POST请求的404 - IIS hosted .net core app is returning 404's for POST requests 如何在 IIS (a) 网站重启和 (b) 重新部署时预热托管在 IIS 中的 asp.net 核心应用程序? - How to warmup an asp.net core app hosted in IIS (a) on website restart and (b) on redeploy? 如何在IIS中本地托管的应用程序的URL中添加自定义目录? - How to add custom directory in URL for app hosted locally on IIS? System.Net.Sockets.SocketException:无法解析主机-Mono上的自托管Nancy应用 - System.Net.Sockets.SocketException: Could not resolve host - Self-hosted Nancy app on Mono ASP.NET Core 3.1 Web API: can it be self-hosted as Windows service with https and use some certificate like IIS? - ASP.NET Core 3.1 Web API : can it be self-hosted as Windows service with https and use some certificate like IIS? 自托管 ASp.net 核心 2.2 应用程序,未找到视图“索引”。 搜索了以下位置 - Self hosted ASp.net core 2.2 app, The view 'Index' was not found. The following locations were searched ASP.NET Identity 2.0,自托管的OWIN,NTLM和应用程序角色持久性 - ASP.NET Identity 2.0, self hosted OWIN, NTLM and application roles persistance AppInsights 仅保存遥测数据,但不使用托管在 IIS 上的 .Net Core web 应用程序保存日志 - AppInsights is only saving telemetry data but not saving logs with a .Net Core web app hosted on an IIS ASP .Net Core 2自包含部署,在不同的应用程序池标识帐户下,IIS中出现502.5错误 - ASP .Net Core 2 Self Contained Deployment giving 502.5 Error in IIS under different App Pool Identity Account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM