How do you get credentials (NetworkCredential) of currently logged in user?

こ雲淡風輕ζ 提交于 2019-12-03 01:07:26

have you tried WindowsIdentity.GetCurrent()?

you could also look at this example... http://www.codeproject.com/KB/vb/Windows_Service.aspx

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

Or you can use web.config:

<identity impersonate="true" />

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.

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.

Have you tried setting the principalpolicy for the appdomain at the start of the application?

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.

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!