【发布时间】:2015-02-05 08:03:51
【问题描述】:
我有一个 WCF 数据服务,它在带有 DataServiceHost 的控制台应用程序中运行
我可以成功启动我的主机并使用此代码查询我的 WCF 数据服务
public void Start()
{
var uri = new Uri("http://localhost:12345/Products");
var host = new DataServideHost(typeof(ProductsDataService), uri);
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
host.Description.Behaviors.Add(new ServiceMetadataBehavior());
if (host.Description.Behaviors.Find<ServiceDebugBehavior>() == null)
host.Description.Behaviors.Add(new ServiceDebugBehavior());
host.Description.Behaviors.Find<ServiceMetadataBehavior()
.HttpGetEnabled = true;
host.Description.Behaviors.Find<ServiceDebugBehavior>()
.IncludeExceptionDetailInFaults = true;
host.AddServiceEndpoint(
new ServiceEndpoint(ContractDescription.GetContract(serviceType))
{
Name = "default",
Address = new EndpointAddress(baseAddress),
Contract = ContractDescription.GetContract(
typeof(IRequestHandler)),
Binding = new WebHttpBinding(),
});
host.Open();
}
我想如何通过基本身份验证或其他方式保护此服务(请注意,我的服务将通过 https 保护)
我找到了很多关于如何使用 IHttpModule 使用 IIS 来保护 DataService 的示例,但是我还发现帖子说我不能将 HttpModules 与我的 DataServiceHost 一起使用。
有人可以给我一个关于如何实现身份验证的提示吗?
【问题讨论】:
-
请参考此链接stackoverflow.com/questions/15410937/…。希望对您有所帮助
-
@LaylaLiuMSFT 谢谢,很高兴知道这是一个选项。由于我不使用
app.config文件,我将不得不通过代码修改绑定(var binding = new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;。但是我找到了一个很好的解决方案,它带有服务器端挂钩,可以让我有更多的控制权(请参阅我的答案),因为我想要无论如何,要针对我的 DbContext 中的用户表进行身份验证,这符合我的需要。
标签: c# wcf authentication odata wcf-data-services