简体   繁体   中英

C# How to convert CredentialCache.DefaultCredentials to PSCredential?

I'm writing ac# program, it will use current user's credential to run a script.

How can I convert CredentialCache.DefaultCredentials object using in access web service to PSCredential object?

Or other better way to create PSCredential without storing username/password?

No guarantees here, but you can at least try something like the following pseudocode

// DefaultCredentials returns an ICredentials reference
// which can then be used to call GetCredentials, 
// and *that* returns a NetworkCredential.
NetworkCredential netCredential = CredentialCache.DefaultCredentials.GetCredentials(..);

// Now, with a NetworkCredential that *might* have a SecurePassword, you 
// could see if that would work to create a PSCredential. I haven't tested this,
// and honestly I'm a bit dubious of the prospects, but its at least a thought.
// There may be some translation necessary to even make that SecurePassword work,
// assuming it is even available in your context, to something the PSCredential
// can use. 

PSCredential psCredential = new PSCredential(netCredential.UserName, 
                                             netCredential.SecurePassword);

As noted, this is untested pseudocode, but hopefully it gives you a push in the right direction. Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM