现象:
asp.net中调用web services时提示错误:
请求因 HTTP 状态 401 失败:Access Denied。
将web services放到另一台服务器问题又不存在该问题。
解决办法:
1.新建方法:
#region GetCredentialCache
/// <summary>
/// 获取CredentialCache
/// 解决访问web service时提示没有权限的问题
/// </summary>
/// <returns></returns>
public static CredentialCache GetCredentialCache()
{
//权限验证web services地址
string WSUrl = RightsServicesUrl;
//Create an instance of the CredentialCache class.
CredentialCache cache = new CredentialCache();
// Add a NetworkCredential instance to CredentialCache.
// Negotiate for NTLM or Kerberos authentication.
cache.Add( new Uri(WSUrl), "Negotiate", new NetworkCredential("域用户名", "域密码", "域"));
return cache;
}
#endregion
2.为web services指定CredentialCache
web services引用名为RightsControl,添加如下代码:
RightsControl rightsControl = new RightsControl();
//为web services指定CredentialCache,最重要的就是这一句
rightsControl.Credentials = GetCredentialCache();
//调用rightsControl的方法