3

这是 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”,即使客户端启动需要身份验证的请求也是如此。

有人知道属性的含义和用法吗?

非常感谢阿尔贝托

4

2 回答 2

3

感谢 marfarma 为我指明了正确的方向。

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

简单总结一下:

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

希望这对其他人有用。

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

于 2013-09-13T05:38:47.863 回答
2

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

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需要身份验证的状态代码?用户名和密码呢。他们人口稠密吗?

相关代码在这里:方法里面的src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.csprotected virtual bool HandleResponseException

于 2013-09-12T20:55:16.933 回答