c# - 如何获取当前Windows登录用户全名而不是域名
问题描述
我想获取当前的 Windows 用户全名,而不是域名。我正在使用下面的代码来获取用户名,但它的名字是带有域名的。
字符串用户名 = Request.ServerVariables["AUTH_USER"];
字符串 a=System.Web.HttpContext.Current.User.Identity.Name;字符串用户名 = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
在上面的 3 个语句中,我越来越像 APAC\san123。但在这里我想要我的全名。这里我在 c# 中使用 mvc。
解决方案
您可以从活动目录中获取用户全名(使用 System.DirectoryServices.AccountManagement):
PrincipalContext context = new PrincipalContext(ContextType.Domain,Environment.UserDomainName);
string loginName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
UserPrincipal user = UserPrincipal.FindByIdentity(context, loginName);
string userName = user.Name; //=user.GivenName + " " + user.Surname;
推荐阅读
- postgresql - 当我们更新行数组时,所有行都被影响了多少次?
- java - 我的 BogoSort 程序可能有逻辑错误
- amazon-web-services - 如何为原始区域内的所有表执行 Glue ETL 作业(从我的原始区域转换为镶木地板到已处理)?
- apache-spark - 在 PySpark 中创建 Hive 模式
- javascript - 如何使用我的 firefox-addon 进行持久的 DOM 更改?
- rust - WindowCreationError“无法创建输入上下文”对 libEGL 和 ggez 意味着什么?
- r - 为什么 R 在创建矩阵时需要交错值?
- json - 如何在 Python 中打开多个 json 文件?
- java - 延迟初始化健全性检查:变量可能尚未初始化
- rabbitmq - MassTransit / RabbitMQ - 为什么跳过这么多消息?