当我在 IIS 上通过 ASP.NET MVC 应用程序进行部署时,我在 Identity 中将我的凭据指定为 DefaultAppPool 中的设置

When I am deploying by ASP.NET MVC application on IIS I am specifying my credentials in Identity as a setting in DefaultAppPool

此外,如果我不提供我的凭据,它会给出一些配置文件错误。

我想要进行 AD 身份验证。但是当从不同的系统登录时,它总是选择我的用户名。我该如何解决?

有什么办法可以创建一个用户组并将其分配给 DefaultAppPool 中的身份,或者我是否需要更改身份验证代码,因为它只从 DefaultAppPool 中提到的身份中获取用户?

我用来验证登录用户的代码如下:

string groupName = ConfigurationManager.AppSettings["groupName"].ToString();
string domainName = ConfigurationManager.AppSettings["domain"].ToString();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx,IdentityType.SamAccountName,System.Security.Principal.WindowsIdentity.GetCurrent().Name);
if (user != null)
{ //code as required }

web.config 文件中的代码:

<appSettings>
  <add key="domain" value="EYDEV" />
  <add key="groupName" value="Domain Users" />
</appSettings> 

WindowsIdentity.GetCurrent() 获取您的应用程序 运行 而非当前用户的凭据。

您需要先启用 Windows 身份验证,方法是将其添加到 web.config 的 system.web 部分:

<authentication mode="Windows" />

有关详细信息,请参阅 Microsoft 的文档(例如确保安装了正确的 IIS 功能):https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/security/authenticating-users-with-windows-authentication-cs

设置完成后,您可以在控制器中使用 User.Identity.Name 来获取当前用户的用户名。