我想學習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不能被激活。
在哪裏我必須把? –
Bv202
2013-03-15 12:25:32
在''下,您的服務行爲。 –
2013-03-15 12:41:29
嘿,我已經添加了我的配置文件。你能看看嗎?謝謝 – Bv202 2013-03-15 12:44:54