[英]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.