HTTP Basic Authentication验证WCF Data Service
2010-08-15 21:08
429 查看
WCF Data Service是OData协议,也是RESTFul Service的一种,上篇文章已经介绍了HTTP Basic Authentication for RESTFul Service,也介绍了服务端如何设置。现在我们来一下客户端怎么样调用。
按照OData and Authentication – Part 6 – Custom Basic Authentication的介绍的方法调用:
[/code]
上面的代码可以完成http方式的调用,如果是https方式调用就会出现错误:
“The remote certificate is invalid according to the validation procedure”
[WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.]
此时需要进行强制验证通过,解决方法是在执行你的代码之前先回调一个名为ServicePointManager的类,每当http客户端的堆栈进行证书验证时,都会检查是否可以回调,如果可以,则执行你你的代码。而要挂接该回调,则必须提供类型RemoteCertificateValidationCallback的一个委托,在调用之前加入如下代码:
[/code]
相关阅读
HttpWebRequest使用SSL连接的时候,处理服务器证书不安全的方法
按照OData and Authentication – Part 6 – Custom Basic Authentication的介绍的方法调用:
[code] Entities ctx = new Entities(new Uri("http://localhost:8080/YupskyDataService.svc"));
var serviceCreds = new NetworkCredential("admin", "admin");
var cache = new CredentialCache();
var serviceUri = new Uri("http://localhost:8080/YupskyDataService.svc");
cache.Add(serviceUri, "Basic", serviceCreds);
ctx.Credentials = cache;
ctx.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequest);
static void OnSendingRequest(object sender, SendingRequestEventArgs e)
{
var creds = "user" + ":" + "password";
var bcreds = Encoding.ASCII.GetBytes(creds);
var base64Creds = Convert.ToBase64String(bcreds);
e.RequestHeaders.Add("Authorization", "Basic " + base64Creds);
}
[/code]
上面的代码可以完成http方式的调用,如果是https方式调用就会出现错误:
“The remote certificate is invalid according to the validation procedure”
[WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.]
此时需要进行强制验证通过,解决方法是在执行你的代码之前先回调一个名为ServicePointManager的类,每当http客户端的堆栈进行证书验证时,都会检查是否可以回调,如果可以,则执行你你的代码。而要挂接该回调,则必须提供类型RemoteCertificateValidationCallback的一个委托,在调用之前加入如下代码:
[code] ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(OnValidationCallback);
public bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
//不管服务端证书怎么样,都通过校验,嘿嘿 return true;
}
[/code]
相关阅读
HttpWebRequest使用SSL连接的时候,处理服务器证书不安全的方法
相关文章推荐
- HTTP Basic Authentication验证WCF Data Service
- WCF HttpBinding 安全解析 (5)Basic验证(IIS宿主)
- WCF BasicHttpBinding 安全解析(6)Digest验证(IIS宿主)
- WCF BasicHttpBinding 安全解析(6)Digest验证(IIS宿主)
- HTTP Basic Authentication for RESTFul Service
- WCF HttpBinding 安全解析 (5)Basic验证(IIS宿主)
- 使用API Key验证WCF Data Service
- HTTP Basic Authentication for RESTFul Service
- Invoke WCF service from Java Client with Authentication (X.509 Certificate) Java 客户端调用WCF服务 需要安全验证
- C#如何调用axis发布的带HTTP Basic Authentication验证的接口配置方式
- Creating CustomBinding for WCFBasicHTTP for SSL and BasicAuthentication in BizTalk 转载自:http://geekswithblogs.net/mipsen
- PHP calling .NET – Running WCF service with basicHttpBinding
- WCF BasicHttpBing With UserName Authentication
- 基本身份验证(HTTP Basic Authentication)及在Ruby on Rails下的实现
- WCF BasicHttpBinding 安全解析(4)windows验证(IIS宿主)
- Silverlight 4 RIA Service dataform Template, 代码选择控件,Validate验证使用技巧
- WCF Service中HttpContext.Current为null的解决办法
- Http Basic Authentication 与 ws-security: 加密,签名,证书等
- 访问需要HTTP Basic Authentication认证的资源的各种语言的实现
- WCF系列(一)【翻译】BasicHttpBinding 和 WsHttpBinding 的不同点