私は基本的なWebサービスのためにAPIキーを実装しています。私はここにある実装を使用しています:https://blogs.msdn.microsoft.com/rjacobs/2010/06/14/how-to-do-api-key-verification-for-rest-services-in-net-4/ 私はそれがすべて実装され、サービス側で正しく設定されていることを知っていますが、私はクライアントからAPIキーを渡す方法がわかりません。要求に応じてWebサービスをデバッグするとき、私はHttpRequestMessageクエリ文字列のために何も返されません。確認するWCFサービスのヘッダーにAPIキーを追加
Webサービスの認証マネージャ:
public string GetAPIKey(OperationContext oc)
{
// get the request
var request = oc.RequestContext.RequestMessage;
// get HTTP request message
var requestProp = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
// get the actual query string
NameValueCollection queryParams = HttpUtility.ParseQueryString(requestProp.QueryString);
// return APIKey if there, NameValueCollection returns null if not present
return queryParams[APIKEY];
}
クライアント消費(重要部品):ここでは、コードです
using (WebClient client = new WebClient())
{
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("APIKey","my_generated_key");
client.Encoding = Encoding.UTF8;
Console.WriteLine(client.UploadString("http://my_local_host/my.svc/myCall", "POST", data));
}
は、デバッグ時には、Webサービスは常にNameValueCollectionの中で空queryParamsを得ていますクエリ文字列が空であるためです。クライアントからのリクエスト中にクエリ文字列に追加するにはどうすればよいですか?