我需要帮助在C#中使用VS 2012 WCF服务应用程序模板将身份验证层OAuth2.0与REST服务集成.在允许客户端(消费者)访问其任何资源之前,此WCF需要发出令牌以进行服务的授权和身份验证.我正在研究三脚认证.很像Twitter,LinkedIn,Google OAuth实现.
已经在互联网上广泛搜索了与OAuth集成的REST WCF API,但没有找到任何合适的潜在客户帮助我.我看了一个旧例子http://weblogs.asp.net/cibrax/archive/2008/11/14/using-the-wcf-oauth-channel-with-an-ado-net-service.aspx
我已经使用此示例与现有的Rest WCF集成.当我运行该服务时,我收到"500内部服务器错误",有时操作只是超时.
以下是导致问题的实现.
我必须添加如下的拦截器,并在.svc Factory ="DemoRESTOAuthService.AppServiceHostFactory"中引用:
class AppServiceHostFactory : System.ServiceModel.Activation.ServiceHostFactory
{
//<summary>
//Factory method called by WCF to create a <see cref="ServiceHost"/>.
//</summary>
//<param name="serviceType">The type of the service to be created.</param>
//<param name="baseAddresses">Collection of base addresses where the <see cref="ServiceHost"/> can listen.</param>
//<returns>An instance of <see cref="ServiceHost"/>.</returns>
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
try
{
Microsoft.ServiceModel.Web.WebServiceHost2 result = new Microsoft.ServiceModel.Web.WebServiceHost2(serviceType, true, baseAddresses);
result.Interceptors.Add(new OAuthChannel.OAuthInterceptor(DemoRESTOAuthService.OAuth.OAuthServicesLocator.Provider, DemoRESTOAuthService.OAuth.OAuthServicesLocator.AccessTokenRepository));
return result;
}
catch(Exception …Run Code Online (Sandbox Code Playgroud)