【问题标题】:Use App Pool Identity and User that running process使用运行进程的应用程序池标识和用户
【发布时间】:2013-10-10 10:42:12
【问题描述】:

我想知道这是否可能或可能的解决方法。我想让我的网站在 IIS 中运行 - 运行该网站的应用程序池将是一个可以查找 AD 的服务帐户。

要获取当前的 App Pool 帐户并设置 Principal 上下文,我有以下内容:

string appPoolAccount = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
// Define Context
PrincipalContext context = new PrincipalContext(ContextType.Domain);

现在用户将在自己的机器上运行该站点 - 当他们在那里启动浏览器时,启动该进程的用户名将是“test123”作为示例 - 因为这将是登录用户。

我需要得到的是他们正在运行浏览器的用户 ID,这样我就可以执行以下操作(第二个参数是否是运行我找到的进程的用户的字符串):

UserPrincipal user = UserPrincipal.FindByIdentity(context , 'test123');

到目前为止,我已尝试从以下位置获取该用户字符串:

string test = Environment.UserName; //Took the App Pool Account
WindowsIdentity wi = WindowsIdentity.GetCurrent(); // Took App Pool account
string test = HttpContext.Current.User.Identity.Name; //bombed out as null

【问题讨论】:

  • 它是一个什么样的应用程序? ASP.NET? WCF 还是其他? HttpContext.Current.User 在非请求处理线程上调用会变成null,这很正常。

标签: c# .net authentication iis active-directory


【解决方案1】:

此代码应该可以工作,但您已在 Web.Config 中启用 Windows 身份验证并禁用匿名身份验证。

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
   string username = System.Web.HttpContext.Current.User.Identity.Name;
}

【讨论】:

  • 正如我所提到的,它会抛出一个空异常,就好像我遇到了断点 HttpConext.Current.User 为 NULL。我已在 IIS 中禁用匿名身份验证 - 我将仔细检查 Web 配置是否已启用 Windows 身份验证
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 2013-02-19
  • 2017-12-15
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 2013-04-09
相关资源
最近更新 更多