我開發WCF服務,basicHttpBinding的,這些服務應該使用.NET 1.1 & .NET 2.0,爲此,我使用basicHttpBinding的訪問。
在舊的ASMX Web服務中,我找到了一個Soap Header(AuthHeader)來驗證用戶的每個請求。
如何在WCF中使用basicHttpBinding進行身份驗證?任何示例或教程都會有幫助。如何在BasicHttpBinding中的WCF服務中進行身份驗證?
NRK
我開發WCF服務,basicHttpBinding的,這些服務應該使用.NET 1.1 & .NET 2.0,爲此,我使用basicHttpBinding的訪問。
在舊的ASMX Web服務中,我找到了一個Soap Header(AuthHeader)來驗證用戶的每個請求。
如何在WCF中使用basicHttpBinding進行身份驗證?任何示例或教程都會有幫助。如何在BasicHttpBinding中的WCF服務中進行身份驗證?
NRK
您可以像切換到WCF之前一樣使用AuthHeader。也許它會更方便你,因爲原則將保持不變。 我在這個解決方案中看到的壞事是純文本密碼傳輸。無論如何,這只是另一種選擇,你可以以某種方式加密/解密密碼。
在這種情況下,你應該實現自己的IDispatchMessageInspector & IClientMessageInspector,像
[AttributeUsage(AttributeTargets.Class)]
public class CredentialsExtractorBehaviorAttribute : Attribute, IContractBehavior, IDispatchMessageInspector
{
#region IContractBehavior implementation.
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint,
DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(this);
}
... empty interface methods impl skipped ...
#endregion
#region IDispatchMessageInspector implementation.
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
int i = request.Headers.FindHeader("username", "sec");
if (-1 != i)
{
string username = request.Headers.GetHeader<string>("username", "sec");
... do smth ...
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
return;
}
#endregion
}
在一個樣品,我把到頭了用戶名,但你可以實現你的包含用戶名和密碼類,並使用它來代替字符串。 在客戶端:
internal class CredentialsInserter : IContractBehavior, IClientMessageInspector
{
private string m_username;
public CredentialsInserter(string username)
{
m_username = username;
}
#region IContractBehavior implementation.
... empty interface methods impl skipped ...
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint,
ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
#endregion
#region IClientMessageInspector implementation.
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageHeader<string> mh = new MessageHeader<string>(m_username);
request.Headers.Add(mh.GetUntypedHeader("username", "sec"));
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
return;
}
#endregion
}
那麼你應該把屬性CredentialsExtractorBehaviorAttribute您的服務實現類。
[CredentialsExtractorBehavior]
public class DummyService : IDummyService
{
... impl ...
}
而在客戶端,你應該做到以下幾點:
using (DummyServiceClient c = new DummyServiceClient("TcpEndpoint"))
{
c.ChannelFactory.Endpoint.Contract.Behaviors.Add(
new CredentialsInserter("_username_"));
c.DummyMethod();
}
首先 - 是的,你可以!這取決於您是使用傳輸還是消息綁定 - 如果您是面向互聯網的用戶,則更有可能使用基於消息的安全性。
不幸的是,對於基於消息的安全性,basicHttpBinding只支持有點痛苦的證書。
wsHttpBinding另一方面也支持用戶名/密碼或其他方法。
你會喜歡這個配置使用用戶名/密碼,客戶證書的wsHttpBinding過基於消息的安全:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsUserName">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="yourservice">
<endpoint name="YourEndpoint"
address=""
binding="wsHttpBinding"
bindingConfiguration="wsUserName"
contract="IYourService" />
</service>
</services>
</system.serviceModel>
<bindings>
下的部分定義綁定配置對於使用消息的安全與用戶名的wsHttpBinding /密碼客戶端憑證。
<service>
下的部分定義了一個示例服務,它使用wsHttpBinding並引用我們剛定義的綁定配置。
在服務器端,您現在可以使用通過電話線發送的用戶名/密碼來驗證您的呼叫者在Active Directory中(每個人呼叫需要一個AD帳戶)還是在ASP.NET成員資格中系統數據庫;或者如果你確實需要,你也可以編寫自己的認證機制。
尋找WCF security at Codeplex - 優秀資源的很多有用信息。
檢查場景here以嘗試將其與您的情況相匹配。每個場景都提供了實施該解決方案所需項目的chceklist。
謝謝,marc_s,我將通過這個名爲CodePlex的材料。所以,我只能對basicHttpBiding中的證書進行驗證。這意味着我無法驗證用戶對用戶名和密碼?在basichttpBiding中有沒有其他的用戶名和密碼進行驗證/驗證? --nrk – nRk 2009-11-24 06:36:43
@nrk:是的,如果您在basicHttpBinding中使用消息安全性,則只能使用證書進行身份驗證。如果您使用傳輸安全性,那麼您可以使用其他認證方式 – 2009-11-24 11:04:49