ServiceStack JsonServiceClient OnAuthenticationRequired

时间:2013-09-12 10:36:45

标签: servicestack

这是关于StackOverflow的第一篇文章,所以如果我做错了,请耐心等待。

我正在使用ServiceStack来创建RESTful Web服务。 在开发示例Windows客户端时,我找到了JsonServiceClient.OnAuthenticationRequired属性,但无法使其正常工作。 我没有找到关于它的文档 - 我假设这是一个委托方法,在服务器需要身份验证时提供用户凭据 - 但我无法让它工作。

这里是我尝试使用的一些示例代码(它是VB.Net,但它对于C#爱好者来说也应该是非常易读的。)

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

即使客户端启动需要身份验证的请求,也不会触发“InteractiveAuthentication”。

有人对财产的含义和用法有所了解吗?

非常感谢你 阿尔贝托

2 个答案:

答案 0 :(得分:3)

Thanx to marfarma指出我正确的方向。

在对源代码进行一些研究之后,我发现只有提供了UserName和Password,ShouldAuthenticate才会返回true。在这种情况下,OnAuthenticationRequired委托被正确触发并且运行得非常好,允许客户端进行身份验证并避免未经授权的异常。

总结一下:

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

希望这对其他人有用。

非常感谢你的帮助 阿尔贝托

答案 1 :(得分:2)

好奇,我检查过来源。当我读到它时,如果WebRequestUtils.ShouldAuthenticate(ex, this.UserName, this.Password)返回true,则在异常处理过程中调用OnAuthenticationRequired函数。该功能如下:

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));
}

您的主机在需要身份验证时是否返回状态代码Unauthorized?那么用户名&密码。它们是否填充了?

相关代码位于protected virtual bool HandleResponseException方法内的src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.cs