我使用Visual Studio 2017和ASP.NET Core 1.2做了一個玩具項目,當我的項目作爲獨立應用程序執行時,我有一些問題檢索用戶的WindowsIdentity,或者在IIS 10(WS2016)中運行,但可以與IIS Express一起正常運行。未使用IIS Express時未設置HttpContext.User.Identity
我一直使用相同的導航器,所以我相信它的配置是正確的。
我做了一箇中間件:
public async Task Invoke(HttpContext context) {
var identity = context.User.Identity;
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(identity)) {
string name = descriptor.Name;
object value = descriptor.GetValue(identity);
_logger.LogDebug("{0}={1}", name, value);
// Displays IsAuthenticated=False, Name=(null), ...
}
var contextIdentity = context.User.Identity as WindowsIdentity;
// Here contextIdentity is null
...
我launchSettings.json樣子:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:1028/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"AVVC.UI.ASP.SMP.Proto.Secur.API": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
在我的.csproj,我已經加入了web.config中的下列規則:
<ItemGroup>
<Content Update="web.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<forwardWindowsAuthToken>True</forwardWindowsAuthToken>
</Content>
</ItemGroup>
並且相應地生成web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath=".\blehbleh.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" />
</system.webServer>
</configuration>
因此,它不能在獨立工作? (我不明白...) – willll
@willll不完全。可以爲使用IIS或WebListener託管的ASP.NET Core應用程序配置Windows身份驗證。因此,您可以使用WebListener(而不是Kestrel,默認情況下使用)來支持自託管方案(但僅適用於Windows)。查看鏈接的文檔以獲取安裝說明。 – Set
它開始工作時,我添加services.Configure(options => {options.ForwardWindowsAuthentication = true;});在ConfigureServices中 –
willll