WCF Services Sample: Authenticate Silverlight Client based on UserName and Password
In the <<WCF Sevices Sample>> series, I have gone through WCF service creation, hosting, transport protocol supporting. By now our service can be accessed by anyone from anywhere, because we did not authenticate the service caller.
The Windows Communication Foundation (WCF) provides many choice for us on service security. For more information about this subject, please refer to MSDN.
In this blog I'd like to talk about WCF service authentication based on username and password. I'd like to use the sample project as before.
1. Implement a Custom Validator Class
Right-click on the "ServiceHost" solution, Add New Project, select "Visual C#" as project type and "Class Library" as template, Input "WcfServiceAuthentication" as project name, then click "OK" button.
Under the "WcfServiceAuthentication" project, right-click on the "References", select "Add Reference...". Click ".Net" tab, select "System.IdentityModel" and "System.ServiceModel" then click "OK" button.
Rename the "Class1.cs" as "MyUserNamePasswordValidator.cs" and make it derived from "System.IdentityModel.Selectors.UserNamePasswordValidator". Next we need to implement the override Validate() method.
Let's take a look at the MyUserNamePasswordValidator class.
using System.IdentityModel.Selectors;
using System.ServiceModel;
namespace WcfServiceAuthentication { public class MyUserNamePasswordValidator : UserNamePasswordValidator { public override void Validate(string userName, string password) { if (userName != "eric.yu" || password != "123456") { throw new FaultException("Invalid Username or Password"); } } } }
2. Config WCF Service with Security Enabled
Now let's turn to the Web.config of ServiceHost project.
Before we can config the service to use "MyUserNamePasswordValidator ", we should add reference to the "WcfServiceAuthentication" project in the ServiceHost project.
The Web.config updated looks like below.
<!-- Service Endpoints -->
userNamePasswordValidationMode="Custom"/>
3. Silverlight Client Code
In order to authenticate Silverlight client caller, we must transfer the username and password to the WCF service. The code looks like below:
HTTPS Scenario
EndpointAddress address = new EndpointAddress(");
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);proxy = new Service1Client(binding, address);
proxy.ClientCredentials.UserName.UserName = "eric.yu";
proxy.ClientCredentials.UserName.Password = "123456";
HTTP Scenario
EndpointAddress address = new EndpointAddress(");
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
proxy = new Service1Client(binding, address);
proxy.ClientCredentials.UserName.UserName = "eric.yu";
proxy.ClientCredentials.UserName.Password = "123456";
4. Run the Application From VS2008
It's time to test our new application now. First we need to rebuild the solution, then update the service reference. If the service reference updated successfully, visit it from the web browser to confirm that it is activated.
Now Press F5 from VS2008, click "Call Service" button and see what will happen. There are 4 test cases:
1) HTTPS and Username/Password are correct;
2) HTTPS and Username/Password are not correct;
3) HTTP and Username/Password are correct;
4) HTTP and Username/Password are not correct;
The case 1) and 3) will return a message string, but the case 2) and 4) will return error message. That's what we expect.
Download the sample code here.
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1034078/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Silverlight+WCF 簡單部署問題集
- python urllib socks5 auth username password 設定Python
- sqlplus / as sysdba報錯ORA-01017: invalid username/password; logon deniedSQLGo
- 【YashanDB資料庫】YAS-02143 invalid username/password, login denied資料庫
- SourceTreet提交時顯示remote: Incorrect username or password ( access token )(4種解決辦法)REM
- grant_type為client_credentials和password二者的區別client
- URL username 屬性
- Silverlight3.0正式版(Silverlight3_Tools)離線安裝
- 【Lintcode】1789. Distinguish UsernameNGUI
- WCF系列教程地址
- 解決安裝Redis之後設定密碼,報(error) ERR Client sent AUTH, but no password is setRedis密碼Errorclient
- WCF雙工通訊
- C# WCF入門C#
- Java與WCF互動(一):Java客戶端呼叫WCF服務 (轉)Java客戶端
- WCF服務端的.NET Core支援專案Core WCF 正式啟動服務端
- Silverlight實用竅門系列:4.Silverlight 4.0新增滑鼠右鍵選單和Silverlight全屏模式的進入退出。【附帶原始碼例項】...模式原始碼
- Silverlight5即將來臨
- Silverlight安裝相關問題
- 02_sample遠控
- Django中的函式make_password、set_password和check_passwordDjango函式
- [WCF許可權控制]利用WCF自定義授權模式提供當前Principal模式
- no-wwwtl622com17008768000-password never prompt for password
- 艾偉:WCF安全之EndPointIdentityIDE
- IIS部署WCF詳細教程
- Silverlight 山寨版樣式瀏覽器瀏覽器
- 2.4.1.2 Nonedefault Services in a CDBNone
- 2.4.1.1 Default Services in a CDB
- 2.4 Overview of Services in a CDBView
- RCE_sample_ctf_questions(ing)
- 探究grid_sample函式函式
- iOS Password AutoFilliOS
- Password Storage - UserDetailsAI
- 學習WCF之路,長期更新
- COMPUTER DATABASES AND ABSTRACTING SERVICES 2Database
- XML安全之Web ServicesXMLWeb
- Amazon Web Services (目錄)Web
- 如何使用 Services.AddDistributedMemoryCache
- Hexo 部落格部署失敗 could not read UsernameHexo
- 征程 6E camera diag sample