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 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;
}
}
何が起こっているのかについての洞察を手伝ってくれる人はいますか?
または、3 脚の OAuth プロバイダーの実装に関する他の適切な例、ポインター、ヒント、またはドキュメントを教えてください。私は文字通り、過去 1 週間、この問題で立ち往生しています。どんな助けでも大歓迎です。
前もって感謝します