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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- WCF Services Sample: Authenticate Silverlight Client based on Custom Security Tokenclient
- WCF Security:authentication based on Username/Password - Part I
- WCF Security:Silverlight authentication for WCF service based on security token
- The password supplied with the username Domain\UserName was not correct. Verify that it was entered ...AI
- 使用Entity Framework和WCF Ria Services開發SilverLight之6:查詢指定欄位Framework
- python urllib socks5 auth username password 設定Python
- Programming WCF Services:資料契約版本控制
- ORA-01017:invalid username/password; logon deniedGo
- Mstsc (Microsoft terminal services client)(遠端桌面)ROSclient
- Silverlight同步(Synchronous)呼叫WCF服務(轉)
- ConfigureGC.pl Reports - Invalid Username/Password. (文件 ID 602750.1)GC
- 【YashanDB資料庫】YAS-02143 invalid username/password, login denied資料庫
- Silverlight中利用WCF獲取客戶端IP客戶端
- SourceTreet提交時顯示remote: Incorrect username or password ( access token )(4種解決辦法)REM
- RMAN-04006: error from auxiliary database: ORA-01017: invalid username/password;ErrorUXDatabase
- [備忘]Redis執行出現Client sent AUTH, but no password is setRedisclient
- AD Administration error:ORA-01017: invalid username/password; logon deniedErrorGo
- sqlplus / as sysdba報錯ORA-01017: invalid username/password; logon deniedSQLGo
- EBS在測試時報 APP-FND-01516: Invalid application username,password,or database.APPDatabase
- Silverlight中非同步呼叫WCF服務,傳入回撥函式非同步函式
- 【DG】搭建DG時報錯:ORA-01017、ORA-17627、ORA-17629 invalid username/password
- grant_type為client_credentials和password二者的區別client
- Your password has expired. To log in you must change it using a client that supports expired passworclient
- Silverlight 呼叫自託管的wcf 報跨域異常的處理跨域
- Javacc sampleJava
- URL username 屬性
- sample a texture as a rendertarget
- netty sampleNetty
- 遠端登入sys使用者報錯:ORA-01017: invalid username/password; logon deniedGo
- SYS使用者遠端登入報錯:ORA-01017: invalid username/password; logon deniedGo
- 十五天精通WCF——第三天 client如何知道server提供的功能清單clientServer
- Configure Client Failover For Dataguard Connections Using DB Services-1429223.1clientAI
- 一步一步學Silverlight 2系列(14):資料與通訊之WCF
- table type usage sample:
- ORA-01017: invalid username/password; logon denied ORA-02063: preceding line from TESTGo
- WCF繫結和行為在普通應用和SilverLight應用一些對比
- 解決安裝Redis之後設定密碼,報(error) ERR Client sent AUTH, but no password is setRedis密碼Errorclient
- Oracle WebLogic Default Password & Change PasswordOracleWeb