none
Windows authentication for WCF Service hosted in Windows Application RRS feed

  • Question

  • Dear All,

    I am new to WCF. I have developed a basic WCF service to return a Helloworld greeting. I am hosting the wcf service in a windows application and have configured the endpoints as in the code below. I have also configured the service to use windows authentication and have also enabled encryption of messages.

    Imports System.ServiceModel
    Imports System.ServiceModel.Description
    Imports AuthenticatedHelloWorldService
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Dim baseAddress As New Uri("http://localhost:8045/HelloWorld")
                Dim Host As New ServiceHost(GetType(HelloWorld))
                Dim tcpbinding = New NetTcpBinding(SecurityMode.Transport)
                'Client credential will be used of windows user
                tcpbinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
                ' When configured for EncryptAndSign protection level, 
                ' WCF both signs the message and encrypts
                'its content. The Encrypted and Signed protection level provides integrity,
                'privacy, and authenticity.
                tcpbinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign
    
                Dim productEndpoint As ServiceEndpoint = Host.AddServiceEndpoint(GetType(IHelloWorld), tcpbinding, "net.tcp://localhost:8000/HelloWorld")
    
                Dim producthttpEndpoint As ServiceEndpoint = Host.AddServiceEndpoint(GetType(IHelloWorld), New BasicHttpBinding(), "http://localhost:8045/HelloWorld")
    
                Host.Open()
                Label1.Text = "The HelloWorld service is running and is listening on:"
                Label2.Text = productEndpoint.Address.ToString() & productEndpoint.Binding.Name
                Label3.Text = producthttpEndpoint.Address.ToString() & producthttpEndpoint.Binding.Name
               
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        
    End Class
    

    I have created a console client and created a service proxy using the below code

    Imports System.ServiceModel
    Imports System.ServiceModel.Description
    Imports AuthenticatedHelloWorldService
    Public Class ServiceProxy
        Inherits ClientBase(Of HelloWorld)
        Implements IHelloWorld
        Public Sub New()
        End Sub
        Public Sub New(ByVal endpointConfigurationName As String)
            MyBase.New(endpointConfigurationName)
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
        Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
            MyBase.New(binding, remoteAddress)
        End Sub
    
        Public Function SayGreetings() As String Implements IHelloWorld.SayGreetings
            Return MyBase.Channel.SayGreetings
        End Function
    End Class

    I am using the proxy in the client to connect to the service using the below code

    Imports System.ServiceModel
    Module Module1
       
        Sub Main()
            
            Try
                Console.WriteLine("Connecting to Service..")
                Dim proxy = New ServiceProxy(New NetTcpBinding(), New EndpointAddress("net.tcp://localhost:8000/HelloWorld"))
                Console.WriteLine(proxy.SayGreetings())
    
    
            Catch ex As Exception
                Console.WriteLine("Service Error:" + ex.Message)
            End Try
            Console.ReadLine()
        End Sub
    
    End Module

    It works fine. But when I am passing the invalid windows credentials  the service is still allowing the access to the client. Please let me know where I am going wrong. The wcf service is not validating the client credentials.

    Imports System.ServiceModel
    Module Module1
       
        Sub Main()
           
            Try
                Console.WriteLine("Connecting to Service..")
                Dim proxy = New ServiceProxy(New NetTcpBinding(), New EndpointAddress("net.tcp://localhost:8000/HelloWorld"))
                proxy.ClientCredentials.Windows.ClientCredential.Domain = "ABCD"
                proxy.ClientCredentials.Windows.ClientCredential.UserName = "Vijay"
                proxy.ClientCredentials.Windows.ClientCredential.Password = "Newww"
                Console.WriteLine(proxy.SayGreetings())
    
    
            Catch ex As Exception
                Console.WriteLine("Service Error:" + ex.Message)
            End Try
            Console.ReadLine()
        End Sub
    
    End Module

    Thanks and regards,

    Noor


    Thanks & Regards, Noor Hussain

    Tuesday, April 2, 2013 4:38 AM

Answers

  • Can you please try it on different PC's. If you are using "localhost" in your endpoint URI, not sure whether this clientcredentials will be applied or not...I mean you are already logged into Windows so it will take the logged in credentials for transport security.

    ViBi

    Tuesday, April 2, 2013 6:11 AM