根据这份文件:
我在web.config
中设置了以下内容<authentication mode="Windows" />
<identity impersonate="false" />
并且还在IIS Express 7.5的applicationhost.config中设置了它
<anonymousAuthentication enabled="false" userName="" />
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
但是System.Threading.Thread.CurrentPrincipal.Identity仍然总是等于经过身份验证的用户的Windows身份,即不是IISExpress.exe在我的开发帐户下运行的帐户。
要清楚,我已登录为帐户A,IIS Express作为帐户A运行,但我使用帐户B调用我的Web服务(在HttpWebRequest上设置凭据),但服务器端代码运行为帐户B,即线程具有此ID,我可以访问网络资源。
我希望执行以帐户A(以及作为服务帐户的prod服务器)进行,并且仅在我需要时进行模拟。
我做错了什么或者这个区域在IISX中没有完美实现?
由于
路
所以,我以为我弄明白发生了什么事;请参阅下面的答案。问题是它似乎正在逆转工作!
string n1 = System.Security.Principal.WindowsIdentity.GetCurrent().Name; // Runtime account.
string n2 = System.Threading.Thread.CurrentPrincipal.Identity.Name; // Calling account.
var winId = (System.Security.Principal.WindowsIdentity)HttpContext.Current.User.Identity;
System.Security.Principal.WindowsImpersonationContext ctx = null;
try
{
bool b = System.IO.File.Exists(@"d:\p\p.txt"); // true (!)
using (ctx = winId.Impersonate())
{
// Now impersonating. Access (local) resources using the identity of the authenticated user.
n1 = System.Security.Principal.WindowsIdentity.GetCurrent().Name; // Calling account.
n2 = System.Threading.Thread.CurrentPrincipal.Identity.Name; // Calling account.
b = System.IO.File.Exists(@"d:\p\p.txt"); // false (!)
}
...
文件夹d:\ p设置为仅允许调用帐户访问,这在DOS中测试时很好,但是从我的Web服务,它有访问权限,我希望这是因为该线程有调用者的权限安全背景,在我开始模仿之前!
Weirder仍然,当我冒充时,我突然失去了它的访问权!
我将在适当的IIS 7.5服务器上创建一个测试项目,看看这是否是IIS Express中的错误。
存在测试的问题已经解决了一半。我删除了文件夹的权限,但文件本身仍然拥有一些权限,.NET访问文件而不遍历文件夹的方式意味着它仍然可以访问它。
现在我
b == false // as expected.
...
b == false // unexpected, after impersonation I should be able to see this file.
我希望假冒行为能够让我访问,但事实并非如此。
我已经放弃了。假冒行为不起作用,我只能假设其网络政策或某些不可发现的隐藏设置。
答案 0 :(得分:2)
知道了。排序。
string n1 = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string n2 = System.Threading.Thread.CurrentPrincipal.Identity.Name;
n1 =流程标识 n2 =来电者的身份
线程的安全上下文具有调用者的身份,这是我没想到的。我认为线程会将流程的上下文传递给它,但这显然不是它的工作方式。
我现在有一个有趣的情况,当我打电话给.Impersonate调用者WindowsIdentity时,我仍然无法访问受调用帐户许可的本地文件,但我会解决这个问题并更新我的答案。
在问题中看到更新