问题
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
回答1:
Curious, I checked the source. As I read it, the OnAuthenticationRequired function is called in the exception handling process, if WebRequestUtils.ShouldAuthenticate(ex, this.UserName, this.Password)
returns true. That function reads as follows:
internal static bool ShouldAuthenticate(Exception ex, string userName, string password)
{
var webEx = ex as WebException;
return (webEx != null
&& webEx.Response != null
&& ((HttpWebResponse) webEx.Response).StatusCode == HttpStatusCode.Unauthorized
&& !String.IsNullOrEmpty(userName)
&& !String.IsNullOrEmpty(password));
}
Is your host returning a status code of Unauthorized
when authentication is required? What about username & password. Are they populated?
The relevant code is here: src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.cs inside the protected virtual bool HandleResponseException
method.
回答2:
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
来源:https://stackoverflow.com/questions/18761937/servicestack-jsonserviceclient-onauthenticationrequired