[英]The remote server returned an error: (401) Unauthorized. wcf httpbinding basic
[英]Enable basic HttpBinding without SSL in WCF
這里的想法是,對於我的測試,在我承諾購買SSL證書之前,我想以非SSL模式啟用WCF服務。 我過去使用此代碼完成了此操作,但是對於我自己來說,無法弄清楚如何將其轉換為web.config文件。
如果有人可以讓我正確指導您如何進行此翻譯,那將不勝感激。
Binding basicBinding = null;
if (RegistryConnectionStringFactory.UseSslForCommunications)
{
basicBinding = new BasicHttpBinding();
(basicBinding as BasicHttpBinding).Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
(basicBinding as BasicHttpBinding).Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
creds.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.MembershipProvider;
creds.UserNameAuthentication.MembershipProvider = Membership.Provider;
}
else
{
HttpTransportBindingElement transport = new HttpTransportBindingElement()
{
AuthenticationScheme = System.Net.AuthenticationSchemes.Basic
};
basicBinding = new CustomBinding(transport);
svcHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new AspNetUsernamePasswordValidator();
svcHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
}
BasicHttpBinding的基於配置的方法有什么問題? 您只需將TransportWithMessageCredential和UserName憑據用於通過HTTPS進行通信,或者將TransportCredentialOnly和Basic憑據用於通過HTTP進行通信。
這是極端情況,但是您可以通過將安全模式設置為“無”來允許匿名和非SSL。 匿名也可能會影響您的測試,因此不建議使用。
<binding name="HttpBasicSecurityConfig">
<security mode="None">
</security>
</binding>
從http://msdn.microsoft.com/en-us/library/ms731172.aspx :
綁定中的傳輸憑據
Type Description
None Specifies that the client does not need to present any credential. This translates to an anonymous client.
因為您的帖子建議這樣做以避免在測試完成之前購買SSL證書,所以我想問:為了節省一些時間,您是否可以使用makecert
創建自己的自簽名證書?
如果是這樣,這些說明可能會有所幫助。
要創建根證書密鑰文件...
makecert -r -pe -n "CN=My Own Authority,O=My Company,C=US" -ss CA -sr CurrentUser -a sha1 -sky signature -sv mycert.pvk mycert.cer
要創建.PFX文件...
makecert -pe -n "CN=localhost" -a sha1 -sky exchange -eku 1.3.6.1.5.5.7.3.1 -ic mycert.cer -iv mycert.pvk -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12 -sv localhost.pvk localhost.cer
pvk2pfx -pvk localhost.pvk -spc localhost.cer -pfx localhost.pfx
然后,使用“證書”管理單元,將mycert.cer
文件導入本地計算機上的“受信任的根證書頒發機構”,以告訴在本地計算機上運行的那些應用程序由您自己的權威機構簽名的任何證書都是受信任的。
接下來,將localhost.pfx
文件導入本地計算機上的“個人”存儲中。 (這樣做可以使IIS可以使用該證書,以便它可以通過您自己的權限將自己聲明為名為“ localhost”的服務器。)
這里有有關如何將.PFX文件導入IIS 7的詳細說明: http : //www.digicert.com/ssl-support/pfx-import-export-iis-7.htm
我今天也在看類似的東西,但是我的知識還不是100%完整。
對於綁定,您將需要執行以下操作:
<bindings>
<customBinding>
<binding name="CustomBinding">
<httpTransport authenticationScheme="Basic"/>
</binding>
</customBinding>
然后,您需要為自定義驗證創建一個serviceBehaviour:
<behavior name="serviceBehavior" >
<serviceAuthorization principalPermissionMode="UseAspNetRoles"roleProviderName="CustomRolesProvider" />
<serviceCredentials>
<userNameAuthentication customUserNamePasswordValidatorType ="AspNetUsernamePasswordValidator [Fully qualified name]" userNamePasswordValidationMode="Custom" />
</serviceCredentials>
顯然沒有經過測試,但是類似的配置僅對我有用,這可能會讓您入門...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.