我一直在关注本教程,以便在我的 WCF 服务中使用传输安全性获得用户名身份验证。然而,本教程提到使用basicHttpBinding
哪个是不可接受的 - 我需要wsHttpBinding
.
这个想法是BasicAuthenticationModule
在 WCF 服务上有一个自定义,它将从 HTTP 请求中读取“授权”标头并根据“授权”标头内容执行身份验证过程。问题是缺少“授权”标头!
我已经IClientMessageInspector
通过自定义行为实现了以操纵传出消息并添加自定义 SOAP 标头。我在BeforeSendRequest
函数中添加了以下代码:
HttpRequestMessageProperty httpRequest = request.Properties.Where(x => x.Key == "httpRequest").Single().Value;
httpRequest.Headers.Add("CustomHeader", "CustomValue");
这应该有效,并且根据许多网络资源,它适用于basicHttpBinding
但不适用于wsHttpBinding
. 当我说“有效”时,我的意思是 WCF 服务已成功接收到标头。
这是检查 WCF 服务端收到的 HTTP 消息的简化函数:
public void OnAuthenticateRequest(object source, EventArgs eventArgs)
{
HttpApplication app = (HttpApplication)source;
//the Authorization header is checked if present
string authHeader = app.Request.Headers["Authorization"];
if (string.IsNullOrEmpty(authHeader))
{
app.Response.StatusCode = 401;
app.Response.End();
}
}
该线程 2011 年 9 月的底部帖子说,这是不可能的wsHttpBinding
。我不想接受这种回应。
作为旁注,如果我使用 IIS 中内置的基本身份验证模块而不是自定义模块,我会得到
参数“用户名”不得包含逗号。** 尝试时出现错误消息
Roles.IsInRole("RoleName")
或 `[PrincipalPermission(SecurityAction.Demand, Role = "RoleName")]
可能是因为我的PrimaryIdentity.Name
属性包含证书主题名称,因为我正在使用TransportWithMessageCredential
基于证书的消息安全性的安全性。
我愿意接受建议以及解决问题的替代方法。谢谢。
更新
看起来,HTTP 标头稍后会在整个 WCF 服务代码中正确读取。
(HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties["httpRequest"]
包含我的自定义标题。但是,这已经是消息级别的了。如何将标头传递给传输身份验证例程?
更新 2
经过一番研究,我得出一个结论,当 Web 浏览器收到 HTTP 状态代码 401 时,它会向我显示登录对话框,我可以在其中指定我的凭据。然而,WCF 客户端只是抛出一个异常并且不想发送凭据。https://myserver/myservice/service.svc
在 Internet Explorer 中访问时,我能够验证此行为尝试使用此链接中的信息进行修复,但无济于事。这是 WCF 中的错误还是我遗漏了什么?
编辑
以下是我system.servicemodel
(来自web.config
)的相关部分 - 我很确定我的配置正确。
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<clientCertificate>
<authentication certificateValidationMode="ChainTrust" revocationMode="NoCheck" />
</clientCertificate>
<serviceCertificate findValue="server.uprava.djurkovic-co.me" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My" />
</serviceCredentials>
<serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="AspNetSqlRoleProvider" />
</behavior>
</serviceBehaviors>
................
<wsHttpBinding>
<binding name="EndPointWSHTTP" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="20480000" maxReceivedMessageSize="20480000" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="20480000" maxStringContentLength="20480000" maxArrayLength="20480000" maxBytesPerRead="20480000" maxNameTableCharCount="20480000" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Basic" />
<message clientCredentialType="Certificate" negotiateServiceCredential="true" algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
............
<service behaviorConfiguration="ServiceBehavior" name="DjurkovicService.Djurkovic">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="EndPointWSHTTP" name="EndPointWSHTTP" contract="DjurkovicService.IDjurkovic" />
</service>
服务返回的异常是:
HTTP 请求未经客户端身份验证方案“匿名”授权。从服务器收到的身份验证标头是“Basic Realm,Negotiate,NTLM”。(远程服务器返回错误:(401) Unauthorized。)