您如何获取当前登录用户的凭据(NetworkCredential)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2319675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get credentials (NetworkCredential) of currently logged in user?
提问by Black Light
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.
我正在编写一些代码来利用 3rd 方组件,并且我需要在开始使用它时提供一个实现 ICredentials 的对象。
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).
...并通过“凭据”,这很好。但我想传递当前用户的凭据(它是 Windows 服务,因此以指定用户身份运行)。
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
谢谢
回答by yamspog
have you tried WindowsIdentity.GetCurrent()?
你试过 WindowsIdentity.GetCurrent() 吗?
you could also look at this example... http://www.codeproject.com/KB/vb/Windows_Service.aspx
你也可以看看这个例子... http://www.codeproject.com/KB/vb/Windows_Service.aspx
回答by Ross Presser
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.
鉴于此原则,操作系统的任何部分都不会明文显示您的用户密码,更不用说愿意将其提供给您了。
回答by Alexan
You need to do impersonation, for example:
您需要进行模拟,例如:
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo();
http://support.microsoft.com/kb/306158
http://support.microsoft.com/kb/306158
Or you can use web.config:
或者你可以使用 web.config:
<identity impersonate="true" />
回答by Jeff
Unfortunately you have to interop with the WMI like this:
不幸的是,您必须像这样与 WMI 互操作:
http://www.codeproject.com/Articles/28161/Using-WMI-to-manipulate-services-Install-Uninstall
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.
您要查询的值是 StartName,它的计算结果类似于“NT Authority\NetworkService”(或您使用的任何内容)。如果您将本文的第二部分与第一部分混合在一起,那么它应该非常简单。
回答by stombeur
Have you tried setting the principalpolicy for the appdomain at the start of the application?
您是否尝试过在应用程序开始时为 appdomain 设置 principalpolicy?
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
Setting this value before accessing the current principal object via the thread makes sure the windows identity is used in this object.
在通过线程访问当前主体对象之前设置此值可确保在此对象中使用 Windows 标识。
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.
编辑 - 我很确定这适用于 DefaultNetworkCredentials。我使用它从 Windows 窗体应用程序访问具有 Windows 身份验证的 Web 服务。
回答by Croket
if you just want to run a process as the current user adds the verb:
"runas " & Environment.UserName
如果您只想在当前用户添加动词时运行一个进程:
"runas " & Environment.UserName
If you want to run the process as admin just wrote "runas"
如果您想以管理员的身份运行该进程刚刚写 "runas"
in vb.net
在 vb.net 中
Dim ps As New System.Diagnostics.ProcessStartInfo("filepath", "arguments")
ps.Verb = "runas" 'run as admin
'ps.Verb = "runas " & Environment.UserName'run as current user, by default
Dim p As System.Diagnostics.Process = System.Diagnostics.Process.Start(ps)
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
如果要获取当前用户的密码,则不能,实际上这是一种不安全的做法。您的服务需要获取我的 Windows 密码机密的权利和目的是什么?例如,就像将手机的密码提供给 whatsapp

