我开发了一个windows服务,它以特定的windows用户身份运行。我使用这个用户,因为它有权限访问我需要在这个windows服务中请求的网站。
问题是,当请求web时,使用CredentialsCache.DefaultNetworkCredentials I使用登录(WindowsIdentity.GetCurrent())到windows的当前用户的凭据,而windows对此web没有访问权限。我
我需要以某种方式传递Windows服务的"Log in as“用户的凭据:
WebRequest request = WebRequest.Create("some url");
// the user running the service can be get from here:
// WindowsIdentity.GetCurrent(), but not the password
request.Credentials = "some code tobtain the user from the service user"如果我使用:
request.Credentials = CredentialCache.DefaultNetworkCredentials;它使用来自windows帐户而不是windows服务帐户的用户。
有什么想法吗?
发布于 2018-09-27 01:24:55
可以从以下位置获取运行该服务的用户: WindowsIdentity.GetCurrent(),但不能获取
如果你能得到当前的WindowsIdentity,你应该能够impersonate它:
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
using (WindowsImpersonationContext impersonatedUser = currentIdentity.Impersonate())
{
// Check the identity.
Console.WriteLine("After impersonation: " + WindowsIdentity.GetCurrent().Name);
// Make the request
WebRequest request = WebRequest.Create("https://google.com");
// etc
}https://stackoverflow.com/questions/52522772
复制相似问题