[英]OAuth 2.0 integrated with REST WCF Service application
我需要使用C#中的VS 2012 WCF Service应用程序模板将身份验证层OAuth2.0与REST服务集成的帮助。 在允许客户端(消费者)访问其任何资源之前,该WCF需要发布用于服务授权和身份验证的令牌。 我正在研究三足式身份验证。 类似于Twitter,LinkedIn,Google OAuth实施。
在Internet上广泛搜索了与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 Internal server error”(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 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 Provider实现提供任何其他合适的示例,指针,技巧或文档,以帮助我。 在过去的一个星期里,我一直困扰着这个问题。 任何帮助表示赞赏。
提前致谢
为什么不使用诸如ServiceStack之类的框架,该框架已经构建了OAuth2身份验证提供程序: https : //github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization
或者,如果您不想使用整个堆栈,请查看他们的代码以了解它与您自己的代码有何不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.