1

我正在编写 ac# 程序,它将使用当前用户的凭据来运行脚本。

如何将CredentialCache.DefaultCredentials在访问 Web 服务中使用的对象转换为PSCredential对象?

或者其他更好的方法来创建 PSCredential 而不存储用户名/密码?

4

1 回答 1

0

这里没有保证,但您至少可以尝试以下伪代码

// 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);

如前所述,这是未经测试的伪代码,但希望它能让您朝着正确的方向前进。祝你好运。

于 2012-07-25T12:51:41.640 回答