Software Technology Tips

We can use messageHeaders to check and authenticate the connection between the Silverlight and WCF service. Operation context of type System.ServiceModel holds the information for the current operation in client and service side.

For sending messages from the client side we will use the OutgoingMessageHeaders of type System.ServiceModel.Channels.MessageHeaders and similarly for receiving header information in service side we will use IncomingMessageHeaders.

Suppose we want to send the username and password from the client and check in the service side we can use messageHeaders..

Here in the client side we can create header with the CreateHeader function with the parameter CreateHeader(name as string,ns as string,value as object)

 

Imports System.ServiceModel.Channels

Dim PersonObj As New Person
PersonObj.UserName = "Soumyap"
PersonObj.Password = "Mindfire"

Dim messageHeadersElementOutgoing As MessageHeaders = OperationContext.Current.OutgoingMessageHeaders
messageHeadersElementOutgoing.Add
(MessageHeader.CreateHeader("Authentication", "", PersonObj))

In the service side we will get the header with the same name and namespace that we have created  the header in the client side using IncomingMessageHeaders and do our service operation .

Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.Runtime.Serialization
Imports System.ServiceModel.Channels

<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements
(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class PersonService

    <OperationContract()> _
    Public Sub DoWork()
       
' Add your operation implementation here
    End Sub

    ' Add more operations here and mark them with <OperationContract()>
    <OperationContract()> _
    Public Function GetPersonData() As String
        Dim PersonObj As New Person

        Dim messageHeadersElementIncoming As MessageHeaders = OperationContext.Current.IncomingMessageHeaders
        Try
            Dim IsAuth As Integer
            IsAuth = messageHeadersElementIncoming.FindHeader("Authentication", "")
            If IsAuth > -1 Then
                PersonObj = messageHeadersElementIncoming.GetHeader(Of [Person])("Authentication", "")
                If PersonObj.UserName = "Soumyap" And PersonObj.Password = "Mindfire" Then
                    Return "Hello"
                End If
            End If
        Catch ex As Exception
            Return Nothing
        End Try

        Return Nothing

    End Function   
End Class

<DataContract()> _
Public Class Person
    Private UserNameStr As String
    Private PasswordStr As String
    <DataMember()> _
    Public Property UserName() As String
        Get
            Return UserNameStr
        End Get
        Set(ByVal value As String)
            UserNameStr = value
        End Set
    End Property
    <DataMember()> _
    Public Property Password() As String
        Get
            Return PasswordStr
        End Get
        Set(ByVal value As String)
            PasswordStr = value
        End Set
    End Property
End Class


Related Tags:

Silverlight

Author: Soumya Patnaik

Silverlight

Let us Connect!

privacy
iso 9001 QA25 Nasscom Red Herring zinnov STPI iso 27001

copyright (c) Mindfire Solutions 2007-2013. Login