【发布时间】:2020-11-16 20:02:57
【问题描述】:
我目前有为 URL 创建代理接口的 Web 服务调用。我需要更新应用程序以接受 Oauth 2.0。是否可以将 Oauth 2.0 与 WCF Webservice 调用一起使用?
这是我的代理接口初始化。我像普通的类初始化一样使用它。
var client = ServiceClient.CreateProxyInterface<MyWebServiceClass>(WebServiceUrl);
在代理接口中,我进行一些授权检查并创建所请求对象的实例并将其返回给客户端
public static TInterface CreateProxyInterface<TInterface>(string ServiceUrl) where TInterface : class
{
var UseClientCertificate = true;
if (ServiceClient.IsUnsecuredHttpService(ServiceUrl, UseClientCertificate))
ServiceUrl = new UriBuilder(ServiceUrl)
{
Scheme = Uri.UriSchemeHttps,
Port = -1
}.Uri.ToString();
var key = TInterface.ToString() + ServiceUrl + UseClientCertificate.ToString();
ChannelFactory myChannelFactory = ServiceClient.FactoryCache[key];
proxy = ((ChannelFactory<TInterface>) mlifChannelFactory1.Factory).CreateChannel();
return proxyInterface;
}
然后客户端可以调用该类中的方法
var address = client.GetAddress(personId);
【问题讨论】:
标签: wcf oauth-2.0 webservice-client channelfactory