Windows 7操作系统.Net Windows服务中的CredentialCache和NetworkCredential [英] CredentialCache and NetworkCredential in .Net windows service, windows 7 OS
问题描述
问题 访问NetworkCredentials,Windows服务应用程序
Problem in accessing NetworkCredentials, Windows service Application
请参阅以下代码:
CredentialCache
CredentialCache
credentialCache = null ;
credentialCache = null;
credentialCache = new
CredentialCache ();
credentialCache = new CredentialCache();
networkCredential =
networkCredential =
(UserId.Trim(),Password.Trim( ));
credentialCache.Add(
credentialCache.Add(
WebRequest.Credentials = credentialCache;
WebRequest.Credentials=credentialCache;
:
= new
NetworkCredential (UserId.Trim(),Password.Trim());得到错误"对象引用未设置为对象的实例"
仅在窗口7操作系统中,并且在Windows XP中正常工作,
请
帮助我。
Please Help me out.
< span style ="font-size:x-small"> 先谢谢
。
&NBSP; 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度> 跨度>
推荐答案
UserId 或密码为空。
在调用 string.Trim()方法之前,你应该检查字符串是否为空。
UserId or Password is null.
You should check if string is null before you call string.Trim() method.
示例:
string username = null;
string password = null;
if (!string.IsNullOrEmpty(UserId))
{
username = UserId.Trim();
}
if (!string.IsNullOrEmpty(Password))
{
password = Password.Trim();
}
CredentialCache credentialCache = new CredentialCache();
NetworkCredential networkCredential = new NetworkCredential(username, password);
credentialCache.Add(new System.Uri(url),"BASIC",networkCredential);
这篇关于Windows 7操作系统.Net Windows服务中的CredentialCache和NetworkCredential的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!