首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以NetworkCredentials用户身份使用Windows服务“登录身份”处理web请求

以NetworkCredentials用户身份使用Windows服务“登录身份”处理web请求
EN

Stack Overflow用户
提问于 2018-09-27 00:59:13
回答 1查看 623关注 0票数 0

我开发了一个windows服务,它以特定的windows用户身份运行。我使用这个用户,因为它有权限访问我需要在这个windows服务中请求的网站。

问题是,当请求web时,使用CredentialsCache.DefaultNetworkCredentials I使用登录(WindowsIdentity.GetCurrent())到windows的当前用户的凭据,而windows对此web没有访问权限。我

我需要以某种方式传递Windows服务的"Log in as“用户的凭据:

代码语言:javascript
复制
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"

如果我使用:

代码语言:javascript
复制
request.Credentials = CredentialCache.DefaultNetworkCredentials;

它使用来自windows帐户而不是windows服务帐户的用户。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2018-09-27 01:24:55

可以从以下位置获取运行该服务的用户: WindowsIdentity.GetCurrent(),但不能获取

如果你能得到当前的WindowsIdentity,你应该能够impersonate它:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52522772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档