WPF 和 WCF 数据服务在查询级别进行身份验证?

问题描述 投票:0回答:2

所以,我发誓我对如何保护 WCF 数据服务完全感到困惑。在这方面,是否有一种简化的检查方法,以确保将数据发送到 WCF 服务的客户端经过身份验证,确保客户端本身是我编写的客户端,而不是某个模拟客户端?

有什么网址可以帮助我解决这个问题吗?

wpf wcf
2个回答
1
投票

我使用 API 密钥通过 HTTPS“保护”我的服务,并且仅允许使用 IIS 访问特定 IP 地址。只需像这样覆盖

OnStartProcessingRequest()
即可:

    protected override void OnStartProcessingRequest(ProcessRequestArgs Args)
    {
        // allow the metadata to be retrieved without specifying an API key by appending $metadata on the end
        if (Args.RequestUri.Segments.Last().Replace("/", String.Empty) != "$metadata")
        {
            // check if a valid API key has been passed in (see Configuration.xml)
            if (!IsValidAPIKey(Args.OperationContext.RequestHeaders["APIKey"])) throw new DataServiceException("Invalid API key");
        }

        base.OnStartProcessingRequest(Args);
    }

    private bool IsValidAPIKey(string PassedAPIKey)
    {
        if (!String.IsNullOrEmpty(PassedAPIKey))
        {
            Guid APIKey;

            // Configuration.APIKeys is just a simple list that reads from an XML file
            if (Guid.TryParse(PassedAPIKey, out APIKey) && Configuration.APIKeys.Exists(x => x.Key == APIKey)) return true;
        }

        return false;
    }

我的 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<ArrayOfAPIKey xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <APIKey Key="ee5346fa-bca5-4236-ae6c-f35ae2f69e0b" ApplicationName="blah" />
</ArrayOfAPIKey>

我的客户端:

base.SendingRequest += (s, e) => { e.Request.Headers.Add("APIkey", "your-api-key-here");  };

0
投票

WCF 数据服务使用普通 WCF 堆栈的常规 authN/authZ 组件。您如何托管您的服务(通常在 IIS 中)以及您使用哪种身份验证方案?

更新:Astoria/WCF 数据服务团队有一个关于 WCF 数据服务和身份验证的优秀博客文章系列: 链接

© www.soinside.com 2019 - 2024. All rights reserved.