【问题标题】:OAuth 2.0 integrated with REST WCF Service applicationOAuth 2.0 与 REST WCF 服务应用程序集成
【发布时间】:2013-02-14 18:15:09
【问题描述】:

在使用 C# 中的 VS 2012 WCF 服务应用程序模板将身份验证层 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 内部服务器错误”,其他时候操作只是超时。

这是导致问题的实现。

我必须添加如下拦截器并在 .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 实施的任何其他合适的示例、指针、提示或文档。在过去的一周里,我真的被这个问题困住了。任何帮助表示赞赏。

提前致谢

【问题讨论】:

标签: c# oauth-2.0 wcf-security


【解决方案1】:

您为什么不使用已经构建了 OAuth2 身份验证提供程序的框架,例如 ServiceStack: https://github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization

或者,如果您不想使用整个堆栈,请查看他们的代码,看看它与您自己的代码有何不同。

【讨论】:

    猜你喜欢
    • 2014-02-05
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多