2017-08-31 98 views
1

我使用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> 

回答

1

它不适用于独立应用程序,因为launchSettings.json文件,您只有iisSettings部分仅在从Visual Studio运行应用程序时使用。

这个json文件保存了与每个调试配置文件相关的项目特定设置,Visual Studio被配置为用来启动应用程序,包括应该使用的任何环境变量。


要使用IIS 10解决第二个问题,你需要设置Windows身份验证您的IIS网站。从Configure Windows Authentication in ASP.NET Core的“自定义身份验证”部分:

  • 禁用匿名身份验证并启用Windows身份验证。

enter image description here

还那么相关:dotnet run - angular - windows authentication - not authenticated

+0

因此,它不能在独立工作? (我不明白...) – willll

+1

@willll不完全。可以为使用IIS或WebListener托管的ASP.NET Core应用程序配置Windows身份验证。因此,您可以使用WebListener(而不是Kestrel,默认情况下使用)来支持自托管方案(但仅适用于Windows)。查看链接的文档以获取安装说明。 – Set

+0

它开始工作时,我添加services.Configure (options => {options.ForwardWindowsAuthentication = true;});在ConfigureServices中 – willll