如何获取当前登录用户的凭据(NetworkCredential)?
我正在编写一些代码来利用第三方组件,并且当我开始使用它时,我需要提供一个实现 ICredentials 的对象。
如果我写以下内容...
var credential = new NetworkCredential("MyUsername", "MyPassword");
...并通过“凭据”,那就没问题了。但我想传递当前用户的凭据(它是 Windows 服务,因此以指定用户身份运行)。
我已经尝试了以下两种方法,但似乎都不起作用(或返回任何内容):
NetworkCredential credential = System.Net.CredentialCache.DefaultCredentials;
NetworkCredential credential = CredentialCache.DefaultNetworkCredentials;
任何人都可以建议如何获取适当的对象,该对象代表运行服务的用户名的凭据吗?
谢谢
I'm writing some code to utilise a 3rd party component, and I need to supply an object which implements ICredentials when I start to use it.
If I write the following...
var credential = new NetworkCredential("MyUsername", "MyPassword");
...and pass "credential", it's fine. But I would like to pass the credentials of the current user (it's a Windows service, so runs as a specified user).
I have tried both of the following, but neither appear to work (or return anything):
NetworkCredential credential = System.Net.CredentialCache.DefaultCredentials;
NetworkCredential credential = CredentialCache.DefaultNetworkCredentials;
Can anyone suggest how to acquire an approriate object, which represents the credentials of the username that the service is running under ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您尝试过 WindowsIdentity.GetCurrent() 吗?
你也可以看看这个例子... http://www.codeproject.com/ KB/vb/Windows_Service.aspx
have you tried WindowsIdentity.GetCurrent()?
you could also look at this example... http://www.codeproject.com/KB/vb/Windows_Service.aspx
您需要进行模拟,例如:
http://support.microsoft.com/kb/306158
或者您可以使用 web.config:
You need to do impersonation, for example:
http://support.microsoft.com/kb/306158
Or you can use web.config:
理想的安全情况是登录用户的密码不存储在内存中的任何位置。它也不存储在磁盘上的任何位置。它仅作为哈希值存在,与人类输入的字符串进行比较。明文存储密码本质上存在安全风险,应尽可能避免。
鉴于这一原则,操作系统的任何部分都不会清楚地知道您的用户密码,更不用说愿意将其提供给您了。
The ideal security situation is that the password of a logged in user is not stored anywhere in memory. It is not stored anywhere on disk either. It exists only as a hash value to be compared against a string entered by a human being. Storing a password in clear is inherently a security risk and should be avoided whenever possible.
Given this principle, there is NO part of the operating system that even HAS your user's password in the clear, much less be willing to give it to you.
不幸的是,您必须像这样与 WMI 进行互操作:
http://www.codeproject.com/Articles/28161/Using-WMI-to-manipulate-services-Install-Uninstall
您要查询的值是 StartName,其计算结果类似于“ NT Authority\NetworkService”(或您正在使用的任何内容)。如果您将本文的第二部分与第一部分结合起来,那么它应该非常简单。
Unfortunately you have to interop with the WMI like this:
http://www.codeproject.com/Articles/28161/Using-WMI-to-manipulate-services-Install-Uninstall
The value you're looking to query for is StartName, which will evaluate to something like "NT Authority\NetworkService" (or whatever you're using). If you mash up the second part of this article with the first part getting it should be pretty straightforward.
您是否尝试过在应用程序启动时设置应用程序域的主体策略?
在通过线程访问当前主体对象之前设置此值可确保在此对象中使用 Windows 标识。
编辑 - 我很确定这适用于 DefaultNetworkCredentials。我用它从 Windows 窗体应用程序访问具有 Windows 身份验证的 Web 服务。
Have you tried setting the principalpolicy for the appdomain at the start of the application?
Setting this value before accessing the current principal object via the thread makes sure the windows identity is used in this object.
edit - I'm pretty sure this works for DefaultNetworkCredentials. I used it to access a web service with windows authentication from a windows forms app.
如果您只想在当前用户添加动词时运行一个进程:
<代码>“runas”&amp; Environment.UserName
如果你想以admin的身份运行进程就
在vb.net中
写
"runas"
如果你想获取当前用户的密码,你不能,事实上它是一种不安全的做法。你们的服务需要什么权利以及出于什么目的获取我的 Windows 密码机密?
例如,就像将您手机的 PIN 码发送给 Whatsapp
if you just want to run a process as the current user adds the verb:
"runas " & Environment.UserName
If you want to run the process as admin just wrote
"runas"
in vb.net
if you want to get the current user password, you can not, in fact it is an unsafe practice.
What right and for what purpose your service needs to get my Windows password secret?
for example is like giving the pin code of your phone to whatsapp