Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack JsonServiceClient OnAuthenticationRequired

Tags:

servicestack

This is the very first post on StackOverflow, so please be patient if am doing anything wrong.

I am using ServiceStack to create RESTful webservices. While developing a sample windows client I have found the JsonServiceClient.OnAuthenticationRequired property, but was unable to get it working. I have found no documentation about it - and I am assuming that this is a delegate method to supply user credentials when the authentication is required by the server - but I have not been able to get it working.

here some sample code I am trying to use (it is VB.Net, but it should be very readable also for C# lovers).

Private _clientAuthenticationRequested As New Action(Of WebRequest)(AddressOf InteractiveAuthentication)

Private Sub Login
....
    _client = New JsonServiceClient(WEBSERVER_ADDRESS)
    _client.OnAuthenticationRequired = _clientAuthenticationRequested
....
End Sub
Private Sub InteractiveAuthentication(SourceRequest As WebRequest)
    SourceRequest.Credentials= ... set some valid credentials
End Sub

The 'InteractiveAuthentication' is never triggered, even when the client starts a request requiring authentication.

Anybody has an idea about the property meanings and usage?

Thank you very much Alberto

like image 376
Piggy69 Avatar asked Aug 03 '26 17:08

Piggy69


1 Answers

Thanx to marfarma for pointing me into the right direction.

After a little of research in the source code I have found that ShouldAuthenticate returns true only if the UserName and Password are provided. In this case, the OnAuthenticationRequired delegate is correctly triggered and running very fine, allowing the client to authenticate and avoid the Unauthorized exception.

Just to summarize:

Private _clientAuthenticationRequested As New Action(Of WebRequest)(AddressOf InteractiveAuthentication)

Private Sub Login
....
    _client = New JsonServiceClient(WEBSERVER_ADDRESS)
    _client.OnAuthenticationRequired = _clientAuthenticationRequested
    'Requided in order to allow ShouldAuthenticate to return true and trigger OnAuthenticationRequired
    _client.UserName = "usr"
    _client.Password = "pwd"
....
'Now - I can execute my request: which will initially fail and trigger the InteractiveAuthentication delegate
response = _client.Get(Of MKXData.MKXPriceResponse)(New MKXData.MKXPricesRequest With {.Ticker = "US10YY"})

End Sub
Private Sub InteractiveAuthentication(SourceRequest As WebRequest)
    'here I start the full authentication procedure (simulating the interactive User to provide his\her credentials)

    Dim authResponse As Auth
    AuthResponse = _client.Send(Of Auth)(New Auth With {.provider = CredentialsAuthProvider.Name,
                                                .UserName = "User",
                                                .Password = "Password",
                                                .RememberMe = True})
    'after successfully executing the authentication, my former request is executed successfully and the results are returned.

End Sub

Hope this will be useful for somebody else.

Thank you very much for your help Alberto

like image 137
Piggy69 Avatar answered Aug 08 '26 08:08

Piggy69



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!