繁体   English   中英

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

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

尝试构建一个Web应用程序,该应用程序将使用Windows身份验证和Active Directory利用单点登录。 我无法获取登录的Windows用户域和用户名。 我尝试过的事情:

  • WindowsIdentity.GetCurrent().Name

    返回IIS APPPOOL\\DefaultAppPool

  • System.Environment.UserName

    返回DefaultAppPool

  • Page.User.Identity.Name

    导致编译错误


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


    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> 

您可能需要调整,具体取决于您尝试访问当前用户的上下文。

例如,从Controller您可以执行以下操作:

User.Identity.GetUserId();

确保您正在使用:

using Microsoft.AspNet.Identity;

请尝试以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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