locked
How to pass username and password via proxy to wcf sync service RRS feed

  • Question

  • I want to restrict access to my wcf - syncservice (hosted in IIS) with username and password.

    Some time ago I had a similar problem when suing SqlCeClientSyncProvider and DbServerSyncProvider (solved here), then I decided to use SqlSyncProvider and SqlCeSyncProvider. I redesigned the application to use these providers. My SyncServiceLibrary runs fine selfhosted when debugging. Now I want to run the syncservice username/password restricted and hosted in IIS, I already created a X509 certifacte and secured the service part. (like described here).

    On the client side I added this line to the CreateProxy to set the MessageCredentialType to "UserName"

    Protected Sub CreateProxy()
        Dim binding As New WSHttpBinding()
        binding.ReaderQuotas.MaxArrayLength = Integer.MaxValue ' 10485760 XXX changed for test
        binding.MaxReceivedMessageSize = Integer.MaxValue  '10485760
        ' new
        binding.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.UserName
        '
        Dim factory As New ChannelFactory(Of ISqlSyncContract)(binding, New EndpointAddress(My.Settings.SqlSyncServiceUri))
        Me.proxy = factory.CreateChannel()
        Me.dbProxy = TryCast(Me.proxy, ISqlSyncContract)
    End Sub

    So far all this seems to work well as I get this error, when I start the sync:

    System.InvalidOperationException: No Username provided. Please specify the username in the ClientCredentials (translated from german).


    My question is now where do I pass the the ClientCredentials to the service?

    I might have a problem understanding the coherence of the different parts involved in the sync process. Here is the code that configs and starts the sync.

    Dim clientprovider As New SqlCeSyncProvider()
    clientprovider.MemoryDataCacheSize = 100 ' 0 = no batching
    clientprovider.Connection = New SqlCeConnection(My.Settings.ClientDBConnectionString)
    Dim destinationProxy As SqlSyncProviderProxy = Nothing
    Dim orchestrator As New SyncOrchestrator()
    AddHandler clientprovider.SyncProgress, AddressOf DisplaySessionProgress
    orchestrator.LocalProvider = clientprovider
    clientprovider.ScopeName = My.Settings.ScopeShared
    destinationProxy = New SqlSyncProviderProxy(My.Settings.ScopeShared, My.Settings.DataSource, My.Settings.InitialCatalog)
    destinationProxy.BatchingDirectory = Path.Combine(System.Environment.CurrentDirectory, "SyncBatch")
    orchestrator.RemoteProvider = destinationProxy
    orchestrator.Direction = SyncDirectionOrder.Download
    Dim stats As SyncOperationStatistics = orchestrator.Synchronize()

    From my understanding the credentials should be passed to the destinationProxy. But I could not figure out how to do this. I am also not sure If I have to add some code to support this.

    Thanks
    Christian

    • Edited by chl-h Monday, September 19, 2011 3:56 PM
    Monday, September 19, 2011 3:54 PM

Answers

  • have you set the username and password somewhere in your code?

    if am not miskaten, your Me.proxy should have a property ClientCredentials.Username.Username / ClientCredentials.Username.Password

    • Marked as answer by chl-h Tuesday, September 20, 2011 9:55 AM
    Tuesday, September 20, 2011 2:07 AM

All replies

  • have you set the username and password somewhere in your code?

    if am not miskaten, your Me.proxy should have a property ClientCredentials.Username.Username / ClientCredentials.Username.Password

    • Marked as answer by chl-h Tuesday, September 20, 2011 9:55 AM
    Tuesday, September 20, 2011 2:07 AM
  • Hi June,

    yes I am setting the username/password in the code. I found the solution:

    I can set credentials like this:


    Dim endPoint = New EndpointAddress(New Uri(My.Settings.SqlSyncServiceUri), EndpointIdentity.CreateDnsIdentity("TheCertificate"))
    Dim factory As New ChannelFactory(Of ISqlSyncContract)(binding, endPoint)
    factory.Credentials.UserName.UserName = "TheUser"
    factory.Credentials.UserName.Password = "thePassword"

    the trick was to do this for factory.

    Thanks

    Christian

     

    Tuesday, September 20, 2011 9:55 AM