2017-08-31 97 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