简体   繁体   中英

How do you get Windows user domain/username with C# Razor on IIS 10?

Trying to build a web app that will utilize single-sign-on using Windows authentication with Active Directory. I'm having trouble getting the logged in Windows user domain and username. Things I have tried:

  • WindowsIdentity.GetCurrent().Name

    Returns IIS APPPOOL\\DefaultAppPool

  • System.Environment.UserName

    Returns DefaultAppPool

  • Page.User.Identity.Name

    Results in a compilation error


    Anonymous Authentication is disabled
    ASP.NET Impersonation is enabled
    Windows Authentication is enabled


    Web.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> 

You might have to adjust, depending on on the context that you are trying to access the current user

eg from a Controller you can do the following:

User.Identity.GetUserId();

make sure that you are using:

using Microsoft.AspNet.Identity;

请尝试以下操作:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM