これは良い代替手段です(ヘッダーで渡します)。次に、カスタムメッセージインスペクターを使用して、以下のコードに示すように、特定のエンドポイントに対するすべてのリクエストに共有キーが存在することを検証できます。
public class StackOverflow_13463251
{
const string SharedKeyHeaderName = "X-API-Key";
const string SharedKey = "ThisIsMySharedKey";
[ServiceContract]
public interface ITest
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string Echo(string text);
[WebGet(ResponseFormat = WebMessageFormat.Json)]
int Add(int x, int y);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
public int Add(int x, int y)
{
return x + y;
}
}
public class ValidateSharedKeyInspector : IEndpointBehavior, IDispatchMessageInspector
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
}
public void Validate(ServiceEndpoint endpoint)
{
}
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
HttpRequestMessageProperty httpReq = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
string apiKey = httpReq.Headers[SharedKeyHeaderName];
if (!SharedKey.Equals(apiKey))
{
throw new WebFaultException<string>("Missing api key", HttpStatusCode.Unauthorized);
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
static void SendRequest(string uri, bool includeKey)
{
string responseBody = null;
Console.WriteLine("Request to {0}, {1}", uri, includeKey ? "including shared key" : "without shared key");
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Method = "GET";
if (includeKey)
{
req.Headers[SharedKeyHeaderName] = SharedKey;
}
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
resp = (HttpWebResponse)e.Response;
}
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
foreach (string headerName in resp.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
}
Console.WriteLine();
Stream respStream = resp.GetResponseStream();
responseBody = new StreamReader(respStream).ReadToEnd();
Console.WriteLine(responseBody);
Console.WriteLine();
Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
Console.WriteLine();
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "");
endpoint.Behaviors.Add(new WebHttpBehavior());
endpoint.Behaviors.Add(new ValidateSharedKeyInspector());
host.Open();
Console.WriteLine("Host opened");
SendRequest(baseAddress + "/Echo?text=Hello+world", false);
SendRequest(baseAddress + "/Echo?text=Hello+world", true);
SendRequest(baseAddress + "/Add?x=6&y=8", false);
SendRequest(baseAddress + "/Add?x=6&y=8", true);
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}