【发布时间】:2018-11-15 16:51:55
【问题描述】:
是否可以自动确定 Windows 用户的用户名,而无需用户使用其用户名和密码登录 Web 应用程序?该用户当然会使用相同的 Active Directory 用户名登录 Windows。
到目前为止,我看到的是,如果我在 IIS 中设置了 Web 应用程序,以 应用程序用户(传递身份验证) 的身份使用具有 NetworkService 的应用程序池连接strong> 身份和 NetworkService 具有 Read & execute、List folder contents 和 Read 访问权限,然后是三者之一事情发生了:
- 启用匿名身份验证和表单身份验证后,网页返回 IIS 的主机名为 Environment.UserName,因此无法自动获取用户的用户名。
- 启用匿名身份验证、启用 ASP.NET 模拟和启用表单身份验证后,网页返回 IUSR 作为 Environment.UserName,因此无法自动获取用户的用户名。
- 启用 Windows 身份验证后,会弹出一个窗口,询问用户的用户名和密码。
我在 Page_Load 上查看 Environment.UserName:
protected void Page_Load(object sender, EventArgs e)
{
//Set username on interface
txtUsername.Text = Environment.UserName;
}
如果我向用户询问凭据,我会使用:
//Determine if user entered the correct username and password
bool AuthenticatedViaAD = DomainContext.ValidateCredentials(loginModule.UserName, loginModule.Password);
我希望在不询问用户用户名的情况下使用的是:
//Get current user
UserPrincipal currentUser = UserPrincipal.FindByIdentity(DomainContext, Environment.UserName);
Page.User.Identity.Name 在用户输入其用户名和密码并启用 Windows 身份验证后进行相应填充:
txtPageUser.Text = Page.User.Identity.Name;
【问题讨论】:
-
您应该坚持使用
Page.Userdocs.microsoft.com/en-us/dotnet/api/…,因为Environment.UserName在几乎所有情况下都不适用于 ASP.NET。 -
谢谢@LexLi。我使用了 Page.User.Identity.Name,但它只在输入用户的用户名和密码后才会填充(只为上面的选项 3 填充)。有没有办法让 Page.User 在用户不输入用户名和密码的情况下进行填充?
标签: c# windows iis active-directory