简体   繁体   English

如何在mvc中获取本地系统域的当前登录用户

[英]How to get current logged in user of Domain of local system in mvc

I have an application for an organization where a user will logged in to his system with domain user, so now I want to get that logged in user.我有一个组织的应用程序,用户将使用域用户登录到他的系统,所以现在我想获取该登录用户。 I tried many methods and get username locally, but my application is on IIS and it gets the IIS user with IIS APPPool\DefaultAppPool while system name and domain get but username not.我尝试了很多方法并在本地获取用户名,但我的应用程序在 IIS 上,它使用IIS APPPool\DefaultAppPool获取 IIS 用户,而系统名称和域却没有。

This is the code I am using for getting username:这是我用来获取用户名的代码:

Request.ServerVariables.Get("logon_user");
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Environment.GetEnvironmentVariable("USERNAME");
System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;

If someone knows how to get local username rather than IIS, please share.如果有人知道如何获取本地用户名而不是 IIS,请分享。 Thanks in advance.提前致谢。

If you have Windows Authentication working, then the current user is available in HttpContext.User .如果您有 Windows 身份验证工作,那么当前用户在HttpContext.User中可用。

User.Identity.Name will be the username. User.Identity.Name将是用户名。

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

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