我對這段簡單的代碼有疑問:
public void OnAuthorization(AuthorizationFilterContext context)
{
ClaimsPrincipal user = _httpContextAccessor.HttpContext.User;
ClaimsIdentity identity = user.Identity as ClaimsIdentity;
string userName = identity.Name; //!!!
_logger.Trace("windows user `{0}` is trying to access the system", userName);
var admins = _configurationRoot.GetSection(ConfigDescription.Admins).Get<List<string>>();
if (!admins.Contains(userName))
{
_logger.Trace("Permission denied.");
context.Result = new RedirectResult("/error/unauthorized", false);
}
}
當我啟動我asp net通過應用IIS Express在Visual Studio一切作業正常。我在這種情況下的日志:
2021-12-25 22:02:53.1783 TRACE windows user `Domain\username` is trying to access the system.
但userName在遠程 IIS 上發布后始終為空。
2021-12-25 19:11:55.2524 TRACE windows user `` is trying to access the system.
2021-12-25 19:11:55.2524 TRACE Permission denied.

我正在嘗試從本地主機和通過域名打開網站,并將其添加到受信任的站點中,但沒有任何幫助。
web.config :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" forwardWindowsAuthToken="true" arguments=".\BlaBla.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
在 IIS 上啟用匿名身份驗證

因為如果沒有,我甚至無法打開這樣的error/unauthorized頁面:

uj5u.com熱心網友回復:
要在 IIS 中啟用 Windows 身份驗證需要確保以下幾點
- 在 IIS 中啟用 Windows 身份驗證
- 在 IIS Web 應用程式中啟用 Windows 身份驗證
1. 在 IIS 中啟用 Windows 身份驗證
我們需要在“Windows 功能”中啟用 Windows 身份驗證(運行命令:optionalfeatures . Win R → optionalfeatures)

2. 在 IIS Web 應用程式中啟用 Windows 身份驗證
然后我們需要為應用程式啟用 Windows 身份驗證。可以在 web.config 中完成,如下所示或在 IIS 中
網頁配置
<system.webServer>
<security>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
資訊系統
在左側節點中選擇應用程式并在功能視圖中選擇“身份驗證”

啟用 Windows 身份驗證和禁用匿名身份驗證。

更多資訊
- IIS Windows 身份驗證
- ASP.NET Core 中的 Windows 身份驗證
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/394734.html
上一篇:Azure登錄-新手機號碼
