我需要帮助在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中引用 工厂= “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 e)
{
throw e;
}
}
}
当我使用日志文件进行调试时,我只能在OAuthChannel程序集的OAuthInterceptor.cs中告诉您引发了异常。我使用了tracelog和fiddler,但除了500内部服务器错误之外,我没有太多帮助理解错误。
public override void ProcessRequest(ref RequestContext requestContext)
{
if (requestContext == null || requestContext.RequestMessage == null)
{
return;
}
Message request = requestContext.RequestMessage;
HttpRequestMessageProperty requestProperty = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
OAuthContext context = new OAuthContextBuilder().FromUri(requestProperty.Method, request.Headers.To);
try
{
_provider.AccessProtectedResourceRequest(context);
OAuthChannel.Models.AccessToken accessToken = _repository.GetToken(context.Token);
TokenPrincipal principal = new TokenPrincipal(
new GenericIdentity(accessToken.UserName, "OAuth"),
accessToken.Roles,
accessToken);
InitializeSecurityContext(request, principal);
}
catch (OAuthException authEx)
{
XElement response = XElement.Load(new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\" version=\"-//W3C//DTD XHTML 2.0//EN\" xml:lang=\"en\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3.org/1999/xhtml http://www.w3.org/MarkUp/SCHEMA/xhtml2.xsd\"><HEAD><TITLE>Request Error</TITLE></HEAD><BODY><DIV id=\"content\"><P class=\"heading1\"><B>" + HttpUtility.HtmlEncode(authEx.Report.ToString()) + "</B></P></DIV></BODY></html>"));
Message reply = Message.CreateMessage(MessageVersion.None, null, response);
HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty() { StatusCode = HttpStatusCode.Forbidden, StatusDescription = authEx.Report.ToString() };
responseProperty.Headers[HttpResponseHeader.ContentType] = "text/html";
reply.Properties[HttpResponseMessageProperty.Name] = responseProperty;
requestContext.Reply(reply);
requestContext = null;
}
}
那里的任何人都可以帮助我了解正在发生的事情吗?
或者,您可以帮助我提供三脚OAuth提供程序实施的任何其他合适的示例,指示,提示或文档。过去一周我一直坚持这个问题。任何帮助表示赞赏。
提前致谢
答案 0 :(得分:1)
为什么不使用已构建OAuth2身份验证提供程序的ServiceStack等框架: https://github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization
或者,如果您不想使用整个堆栈,请查看他们的代码,看看它与您自己的代码有什么不同。