[英]Basic authentication and WCF
我正在嘗試學習WCF,但是我真的不明白我該怎么做。 我有一個包含用戶名和密碼的數據庫,用戶在使用該服務之前應先進行身份驗證。
現在,用戶名和密碼已進行硬編碼:
class UsernameAuthentication : UserNamePasswordValidator
{
/// <summary>
/// When overridden in a derived class, validates the specified username and password.
/// </summary>
/// <param name="userName">The username to validate.</param><param name="password">The password to validate.</param>
public override void Validate(string userName, string password)
{
var ok = (userName == "test") && (password == "test");
if (ok == false)
throw new AuthenticationException("username and password does not match");
}
}
我的服務很簡單:
public class Service1 : IService1
{
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
}
我的問題是:為了使這項工作有效,我到底需要在web.config文件中進行哪些更改? 我看過一些教程,但是並不太了解所需的更改。
另外,我正在嘗試做的-在用戶訪問服務之前對其進行身份驗證,這是正確的方法嗎?
謝謝
編輯:我的配置文件:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Binding1">
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService1.UsernameAuthentication, service1" />
</serviceCredentials>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
錯誤:service1.svc無法激活。
您必須在web.config中指定將使用用戶名/密碼憑據,並使用自定義密碼驗證器。
服務的綁定應設置一種安全性類型(“ Transport
或“ Message
,最適合您),對於這種安全性,必須設置要使用的憑據(用戶名和密碼)。
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="Binding1" ...>
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
其中...
表示您的服務特有的許多其他設置。
考慮到只有某些類型的綁定和安全模式支持這種類型的憑據,但是MSDN擁有您可能需要的所有信息。
如果未將憑據設置為用戶名和密碼,則不會以這種方式對用戶進行身份驗證。
要告訴服務使用您的密碼驗證器,您需要添加以下內容:
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Microsoft.ServiceModel.Samples.CalculatorService.CustomUserNameValidator, service" />
</serviceCredentials>
.....
</serviceBehaviors>
</behaviors>
其中Microsoft.ServiceModel.Samples.CalculatorService
是您具有自定義驗證器的名稱空間, CustomUserNameValidator
是自定義驗證器(在您的情況下為UserNamePasswordValidator
),而service
是service
的名稱。
否則,該服務將需要一個默認驗證器,例如ASP.NET Membership Provider。
服務憑證必須放在您的服務行為中。
另外,不要忘記將行為鏈接到服務定義。
<services>
<service behaviorConfiguration="ServiceBehavior" name="ServiceName">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="Binding1" contract="ContractName" />
....
</service>
</services>
注意 :web.config中還有許多未顯示的設置。 元素的名稱僅是定向的。 這只是為了使用戶名憑據起作用。
您可以查看MSDN,因為他們對此有很多偉大的教程,像這樣一個http://msdn.microsoft.com/en-us/library/aa702565.aspx , http://msdn.microsoft.com/en-us/ library / aa354513.aspx 。
是的,實際上,如果您以正確的方式進行配置,它將在授予客戶端(用戶,客戶端服務)運行服務方法權限之前對其進行身份驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.