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