在asp.net中,当identity impersonate="true "时,如何获得Windows用户的名字?

 2023-02-17    430  

问题描述

我正在创建一个Intranet ASP.NET MVC应用程序,公司中每个人都应该访问.我需要运行为数据库访问等冒充的网站,但是我想知道每个用户是谁.

当我看Page.User.Identity.Name时,它是空白的.即使网站模仿了网站,也可以获取用户的Windows帐户名称吗?

在asp.net中,当identity impersonate="true "时,如何获得Windows用户的名字?

编辑:
这是更多信息.我在IIS 6中有一个网站,并启用了匿名访问.该网站正在具有可以访问数据库的系统帐户下运行(因为所有员工都无法访问数据库).

我的web.config具有<authentication mode=”Windows” />和<identity impersonate=”true”/>

我的目标是用户不必登录 – 这些事实是将其登录到我们的网络(以及该站点不在外部IP上的事实)足够身份验证.我想知道用户是谁,以跟踪他们所做的更改,等等.

推荐答案

with <authentication mode=”Windows”/>在您的应用程序中,在IIS中启用了匿名访问,您将看到以下结果:

System.Environment.UserName: Computer Name
Page.User.Identity.Name: Blank
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name

使用<authentication mode=”Windows”/>在您的应用程序中,”匿名访问”禁用,并且仅在IIS中使用”集成的Windows身份验证”,您将看到以下结果:

System.Environment.UserName: ASPNET (user account used to run ASP.NET service)
Page.User.Identity.Name: Domain\ Windows Account Name 
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name\ASPNET

使用<authentication mode=”Windows”/>和<identity impersonate =”true”/>在您的应用程序中,”匿名访问”禁用,并且仅在IIS中使用”集成的Windows Authentication”,您将看到以下结果:

System.Environment.UserName: Windows Account Name 
Page.User.Identity.Name: Domain\ Windows Account Name 
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Domain\ Windows Account Name

其他推荐答案

尝试此

System.Security.Principal.WindowsIdentity.GetCurrent().Name

它应该返回带有用户登录名的字符串

其他推荐答案

我只是想发布我的修复程序,因为没有人对此表示任何意见.

当我将网站发布到服务器上时,我遇到了同样的问题,但没有在本地.所有设置都是相同的.但是,在IIS中,”默认网站”从未被关闭.即使没有与之相关的网站,它仍在运行和拦截流量.默认验证在默认值中打开了,但在我的网站上关闭了端口80的运行.似乎我的网站关闭了…自从默认值打开以来,所有流量都已打开.到端口80.

禁用默认网络解决了问题.还将端口更改为8080 Works.

我希望这对某人有帮助.

以上所述是小编给大家介绍的在asp.net中,当identity impersonate="true "时,如何获得Windows用户的名字?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/34252.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。