WCF API и API Key авторизация - PullRequest
       12

WCF API и API Key авторизация

2 голосов
/ 02 января 2012

Написал или начал писать службу отдыха WEB API в WCF. Все идет относительно хорошо. Однако я столкнулся с небольшой проблемой. Я реализовал это;

http://blogs.msdn.com/b/rjacobs/archive/2010/06/14/how-to-do-api-key-verification-for-rest-services-in-net-4.aspx

Для проверки ключа. (Я не уверен, что это правильный подход к веб-API WCF, так как он больше похож на реализацию остальных сервисов).

Во всяком случае, это похоже на работу. Однако, если ключ API не указан, исключение не отображается в браузере. То есть если я предоставлю ключ, он вернется правильно, если я не предоставлю, он просто показывает пустую страницу.

  private static void CreateErrorReply(OperationContext operationContext, string key)
    {
        // The error message is padded so that IE shows the response by default
        using (var sr = new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + APIErrorHTML))
        {
            XElement response = XElement.Load(sr);
            using (Message reply = Message.CreateMessage(MessageVersion.None, null, response))
            {

                HttpResponseMessageProperty responseProp = new HttpResponseMessageProperty() { StatusCode = HttpStatusCode.Unauthorized, StatusDescription = String.Format("'{0}' is an invalid API key", key) };
                responseProp.Headers[HttpResponseHeader.ContentType] = "text/html";
                reply.Properties[HttpResponseMessageProperty.Name] = responseProp;
                operationContext.RequestContext.Reply(reply);
                // set the request context to null to terminate processing of this request
                operationContext.RequestContext = null;

            }
        }
    }

Вместо того, чтобы показать ошибку, результатом является пустой ответ. Кто-нибудь может помочь?

...