如何在IIS 10上使用c# Razor获得Windows用户域/用户名?

本文关键字:用户 Windows 获得 Razor IIS | 更新日期: 2023-09-27 18:13:38

尝试构建一个web应用程序,将使用Windows身份验证与活动目录利用单点登录。我在登录Windows用户域和用户名时遇到了麻烦。我尝试过的事情:

  • WindowsIdentity.GetCurrent().Name

    返回IIS APPPOOL'DefaultAppPool

  • System.Environment.UserName

    返回DefaultAppPool

  • Page.User.Identity.Name

    导致编译错误


    禁用匿名认证
    ASP。. NET模拟已启用
    Windows身份验证已启用


    . config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="EnableSimpleMembership" value="false" />
      </appSettings>
      <system.web>
        <compilation debug="false" targetFramework="4.6.1">
          <assemblies>
            <add assembly="System.Net.Http, Version=4.1.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
            <add assembly="Microsoft.Web.Administration, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          </assemblies>
        </compilation>
        <httpRuntime targetFramework="4.6.1" />
        <authentication mode="Windows" />
        <authorization>
          <deny users="?" />
        </authorization>
        <identity impersonate="true" />
      </system.web>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Security.Cryptography.X509Certificates" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Win32.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
        </modules>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <remove name="OPTIONSVerbHandler" />
          <remove name="TRACEVerbHandler" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
      </system.webServer>
    </configuration>
    

如何在IIS 10上使用c# Razor获得Windows用户域/用户名?

您可能需要调整,这取决于上下文您正在尝试访问当前用户

。从Controller中,您可以执行以下操作:

User.Identity.GetUserId();

确保你使用的是:

using Microsoft.AspNet.Identity;

尝试如下:

var windowsIdentity = System.Web.HttpContext.Current.User.Identity as WindowsIdentity;