Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用运行进程的应用程序池标识和用户_C#_.net_Authentication_Iis_Active Directory - Fatal编程技术网

C# 使用运行进程的应用程序池标识和用户

C# 使用运行进程的应用程序池标识和用户,c#,.net,authentication,iis,active-directory,C#,.net,Authentication,Iis,Active Directory,我想知道这是可能的还是可能的解决办法。我想让我的网站在IIS中运行-运行该网站的应用程序池将是一个可以查找广告的服务帐户 要获取当前应用程序池帐户并设置主体上下文,我有以下选项: string appPoolAccount = System.Security.Principal.WindowsIdentity.GetCurrent().Name; // Define Context PrincipalContext context = new PrincipalContext(ContextTy

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

要获取当前应用程序池帐户并设置主体上下文,我有以下选项:

string appPoolAccount = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
// Define Context
PrincipalContext context = new PrincipalContext(ContextType.Domain);
现在,用户将从他们自己的机器上运行该站点-当他们启动浏览器时,启动该进程的用户名将是“test123”作为示例-因为这将是登录用户

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

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

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

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

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

正如我提到的,它会抛出一个null异常,就好像我遇到了一个断点HttpConext.Current.User为null一样。我已经在IIS中禁用了异常身份验证-我将仔细检查web配置是否已启用windows身份验证它是什么类型的应用程序?ASP.NET?WCF还是其他
HttpContext.Current.User
如果在非请求处理线程上调用它,则会变成
null
,这很正常。
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
   string username = System.Web.HttpContext.Current.User.Identity.Name;
}