简体   繁体   English

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

[英]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. 尝试构建一个Web应用程序,该应用程序将使用Windows身份验证和Active Directory利用单点登录。 I'm having trouble getting the logged in Windows user domain and username. 我无法获取登录的Windows用户域和用户名。 Things I have tried: 我尝试过的事情:

  • WindowsIdentity.GetCurrent().Name

    Returns IIS APPPOOL\\DefaultAppPool 返回IIS APPPOOL\\DefaultAppPool

  • System.Environment.UserName

    Returns DefaultAppPool 返回DefaultAppPool

  • Page.User.Identity.Name

    Results in a compilation error 导致编译错误


    Anonymous Authentication is disabled 匿名身份验证已禁用
    ASP.NET Impersonation is enabled 启用了ASP.NET模拟
    Windows Authentication is enabled Windows身份验证已启用


    Web.config 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: 例如,从Controller您可以执行以下操作:

User.Identity.GetUserId();

make sure that you are using: 确保您正在使用:

using Microsoft.AspNet.Identity;

请尝试以下操作:

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

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

相关问题 在ASP.Net C#中获取用户名和域(域\\用户) - get the username and domain (domain\user) in ASP.Net c# 您如何从C#中公司的域/网络设置中获取用户信息? - How do you get user info from a company's domain/network settings in C#? 如何在Windows10快速访问C#中获取所有目录的列表 - How do you get a list of all directories in Windows10 Quick Access c# 在Windows 10 C#UWP通用Windows应用程序中获取UserName - Get UserName in a Windows 10 C# UWP Universal Windows app 在IIS Web应用程序中,如何获取Windows用户名? -不是进程用户名 - in an IIS web app, how do I get the windows user name? - not the process username "如何获取用户名@域格式的当前 Windows 用户名?" - How do I get the current windows user's name in username@domain format? 使用 c# 和 IIS 10 进行域身份验证 - Domain authentication with c# and IIS 10 Razor Page C# ASP.NET Core 获取 Windows 用户名(Windows 身份验证) - Razor Page C# ASP.NET Core get Windows Username (Windows Auth) 如何使用 Razor 在 C# 中传递数组 - How do you pass an array in C# using Razor Pages 如何在 C# 控制台应用程序中获取当前 Windows 用户名? - How do I get Current Windows Username in C# Console Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM