【问题标题】:Get domain user name after host site on server side在服务器端的主机站点之后获取域用户名
【发布时间】:2019-12-12 05:44:22
【问题描述】:

我有一个 MVC 站点,我想检查打开此站点的用户名并根据用户名做出决定。无论如何,我编写了下面的代码并正确运行并正确显示用户名,但是当我在服务器端将 MVC 项目发布到 IIS 时,我无法获取用户名,我看到 IISAPPPOOL 或 NT AUTHORITYIUSR。我搜索了但我找不到任何东西。即使我在我网站的功能视图的身份验证部分中也找不到 Windows 身份验证(我在 Web 服务器下面的安全性下安装了 Windows 身份验证)

web.config:

     <?xml version="1.0" encoding="utf-8"?>
    <configuration>
   <configSections>
    .
   </configSections>

      .
   </appSettings>
   <system.web>
        <compilation debug="true" targetFramework="4.5.2" />
       <httpRuntime targetFramework="4.5.2" />
       <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
     </httpModules>
      </system.web>
     <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
     </dependentAssembly>
     <dependentAssembly>
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
     </dependentAssembly>
     <dependentAssembly>
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
     <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
    </dependentAssembly>
    </assemblyBinding>
     </runtime>
   <system.codedom>
   <compilers>
  <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
  <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
  </system.codedom>
  <system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
   <modules>
  <remove name="ApplicationInsightsWebTracking" />
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
  </system.webServer>
  <connectionStrings>
.
  </connectionStrings>
 <entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
   <parameters>
    <parameter value="mssqllocaldb" />
  </parameters>
  </defaultConnectionFactory>
   <providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
   </providers>
  </entityFramework>
 <system.web>
  <customErrors mode="Off"/>
  <authentication mode="Windows"/>


<identity impersonate="false" />

 </system.web>
  </configuration>

代码:

    System.Security.Principal.WindowsIdentity context = 
    System.Security.Principal.WindowsIdentity.GetCurrent();
        var domainName = context.Name;  ---> show domain/username in local and IISAPPPOOLL after host on IIS

     WindowsIdentity identity = 
     System.Web.HttpContext.Current.Request.LogonUserIdentity;
     string domainName1= identity.Name;--> show NT AUTHORITYIUSR

或在其他代码中用于在本地显示域名中获取域名,但在 IIS 端显示 domain.local 的主机之后..

如何在 IIS 上的主机站点后获取域用户名?

【问题讨论】:

    标签: asp.net-mvc authentication iis config


    【解决方案1】:

    即使我在我网站的功能视图的身份验证部分中也找不到 Windows 身份验证(我在 Web 服务器下方的安全性下安装了 Windows 身份验证)

    根据你的描述,我建议你可以先重新检查你的服务器角色和规则,确保你已经为本地IIS安装了windows认证。另外,我建议你可以尝试停止IIS服务并关闭IIS管理控制台并启动IIS服务并打开IIS管理控制台。

    其次,我建议您可以尝试在 IIS 上禁用 Web 应用程序的匿名身份验证。因为匿名身份验证将使用 NT AUTHORITYIUSR 作为用户帐户名。如果禁用它,它将使用域帐户作为用户帐户类型。

    更多关于如何修改认证,你可以参考下面的配置。

    <system.webServer>
        <security>
            <authentication>
                <windowsAuthentication enabled="true" />
                <anonymousAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
    

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 2014-08-09
      • 2015-07-08
      • 1970-01-01
      • 2019-05-07
      • 2021-01-22
      • 2016-03-24
      • 1970-01-01
      • 2014-07-28
      相关资源
      最近更新 更多