Showing posts with label Tech Altum. Show all posts
Showing posts with label Tech Altum. Show all posts

Sunday, 21 July 2013

Message Security in WCF using username client credential

Message Security in WCF

There are two types of security in WCF. One is the security of Data and second is the security of medium through which message travel.

When we talk about the security of data then it is achieved by message security and if we talk about the security of medium through which message travel which is protocol security can be achieved by transport level security.

In this article I defined how to achieve message level security. There of different type of client credential and using this client credential we achieve message security. I am using wsHttpBinding to achieve message level security

Type of client Credential in message security
1.     None
2.     Windows
3.     Username
4.     Certificate
5.     Issued token

In this example I am using client credential username.

Following are the steps to implement the message security using client credential username

Step 1:-

Create a class and inherit usernamepasswordvalidator class in it. This class will be found on System.IdentityModel.Selectors and override the method validate and verify the username and password.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IdentityModel.Selectors;
using System.ServiceModel;

public class Credentioal:UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName == "isha" && password == "isha123")
        { }
        else
        {

            throw new FaultException("Wrong userid and pwd");
        }
    }
}

Step 2

Wednesday, 27 March 2013

WCF Lesson 4:-Fault Contract in WCF


Fault Contract in WCF

With the help of fault contract we can describe how to exchange error information from service to client. If we get any error in the service then we can pass this error information from service to client with the help of Fault contract.

For Example:-

Everybody is familiar with the error DivideByZero. It occurs when we try to divide a number with zero, It occurs this exception. Same example I am using here.

I create Error class which will be used to pass to the client at the time of error occur:-

[DataContract]
public class Error
{
    [DataMember]
    public string ErrorMessage;

    [DataMember]
    public string ErrorMethod;

}

WCF Lesson 3:-Message Contract in WCF


Message Contract in WCF

Earlier we discuss two contract i.e. service contract and data contract. Now in this article I will discuss message contract.

In message contract data is sent in the format of SOAP message which in the form of header and body. With the help of message header we can implement security by passing credential in header part.

For Example:-

Create wcf service and add class to implement message contract.

[MessageContract]
public class EmployeeRecord
{
    [MessageHeader]
    public string token;

   
    [MessageBodyMember]
    public string name;
  
   [MessageBodyMember]
    public int empid;
}