【发布时间】:2017-08-31 18:31:39
【问题描述】:
我使用 Visual Studio 2017 和 ASP.NET Core 1.2 做了一个玩具项目,当我的项目作为独立应用程序或在 IIS 10 (WS2016) 中执行时,我在检索用户的 WindowsIdentity 时遇到了一些问题,但可以正常使用IIS Express。
我一直使用同一个导航器,所以我相信它的配置是正确的。
我做了一个中间件:
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>
【问题讨论】:
标签: asp.net-core single-sign-on windows-authentication