[英]Windows authentication in asp.net 5
我正在 ASP .NET 5、MVC 6 中构建 Intranet 应用程序。我想知道如何启用 Windows 身份验证。? 默认项目模板仅支持个人用户帐户。
Mark 的回答在 ASP.Net RC1 中仍然有效。 还有一些额外的步骤可以将它们联系在一起(我没有足够的声誉来评论他的解决方案):
将以下使用添加到 Startcup.cs:
using Microsoft.AspNet.Http.Features; using Microsoft.Net.Http.Server;
在app.UseMvc之前的Configure方法中添加Mark的代码片段:
// If we're self-hosting, enable integrated authentication (if we're using // IIS, this will be done at the IIS configuration level). var listener = app.ServerFeatures.Get<WebListener>(); if (listener != null) { listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM; }
要对此进行调试,您需要在project.json
添加 WebListener 运行目标,正如 Mark 在不同的答案中指出的那样:
"commands": { "weblistener": "Microsoft.AspNet.Server.WebListener --config hosting.ini", "web": "Microsoft.AspNet.Server.Kestrel" },
选择 weblistener 而不是 web (Kestrel) 的 IIS Express 来调试您的应用程序。
除了此处仅适用于 IIS 托管的其他答案之外,您还可以通过在 Startup.cs Configure
添加以下内容,在自托管 ASP.NET 5 项目(针对 beta 7 和 beta 8 进行测试)中启用 Windows 身份验证方法,在app.UseMvc
或您希望保护的类似之前:
BETA 8 更新
// If we're self-hosting, enable integrated authentication (if we're using
// IIS, this will be done at the IIS configuration level).
var listener = app.ServerFeatures.Get<WebListener>();
if (listener != null)
{
listener.AuthenticationManager.AuthenticationSchemes =
AuthenticationSchemes.NTLM;
}
先前对 Beta 7 的回答
// If we're self-hosting, enable windows/integrated authentication.
// For IIS, this needs to be configured in IIS instead, and the
// following will have no effect.
if ((app.Server as ServerInformation) != null)
{
var serverInformation = (ServerInformation)app.Server;
serverInformation.Listener.AuthenticationManager.AuthenticationSchemes =
AuthenticationSchemes.NTLM;
}
改编自官方MusicStore 示例。
如果您正在使用带有 IIS Express 的 Visual Studio 2015 进行调试,您现在可以通过项目的调试属性页面中的复选框打开 Windows 身份验证,而不是弄乱applicationhost.config
文件。 我无法让 web.config 解决方案用于 IIS Express 调试,它会引发有关配置在该级别无效的错误。 请注意,这目前在 beta 8 中不起作用 - 请参阅此问题
$(ProjectDir)\\Properties\\launchSettings.json
文件将在针对 IISExpress 进行适当调试时触发 Visual Studio 生成web.config
文件,该文件将根据启动设置设置<authentication/>
节点。
下面是一个示例launchSettings.json
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:65070/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"Hosting:Environment": "Development"
}
},
"web": {
"commandName": "web",
"environmentVariables": {
"Hosting:Environment": "Development"
}
}
}
}
但也使用扩展app.UseIISPlatformHandler();
而不是操纵听众。 该扩展程序将设置一个中间件,该中间件将自动请求 NTLM 并从 IIS 转换相应的句柄。
部署到 IIS 时,如果您使用WebListener
,则必须自己将authentication
节点添加到web.config
。 如果您使用HttpPlatformHandler
(我个人推荐)并代理到forwardWindowsAuthToken="true"
,请将forwardWindowsAuthToken="true"
添加到web.config
的httpPlatform
节点。
使用 IIS 托管,您可以将 web.config 文件添加到您的 wwwroot 目录,其中包含应用程序的 IIS 配置。
网页配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
</configuration>
我做了我在互联网上找到的一切,没有人工作。 所以,我查看了 aspnet 4.5 配置文件,我看到它使用:
<IISExpressAnonymousAuthentication>disabled</IISExpressAnonymousAuthentication>
<IISExpressWindowsAuthentication>enabled</IISExpressWindowsAuthentication>
在 .csproj 文件上,我只是复制到 aspnet 5 的 .xproj 文件并且它起作用了。
对于来自空的 Web 应用程序的 RC1 和 IISExpress:
Properties
Debug
选项卡,选中Enable Windows Authentication
这影响~/Properties/launchSettings.json
如下:
"windowsAuthentication": true,
"anonymousAuthentication": false,
如果要在当前Web项目上启用Windows身份验证:
在解决方案资源管理器上,在网站上右击并选择“属性窗口”
将“匿名身份验证”设置为“禁用”
并设置“ Windows身份验证”
运行项目,一切都会好起来。
您需要手动配置 IIS Express(在 VS2015 CTP6 中)。 为此,请编辑 applicationhost.config 文件。 (C:\\Users\\您的用户名\\Documents\\IISExpress\\config\\applicationhost.config)
在配置标签中添加:
<location path="{your site name}">
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
</location>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.