gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:14:38 25 4
gpt4 key购买 nike

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

任何可以帮助我解码此问题的 URL?

最佳答案

我正在使用 API key 通过 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");  };

关于WPF 和 WCF 数据服务在查询级别进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3430208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com