ServiceStack JsonServiceClient OnAuthenticationRequired [英] ServiceStack JsonServiceClient OnAuthenticationRequired

查看:20
本文介绍了ServiceStack JsonServiceClient OnAuthenticationRequired的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 StackOverflow 上的第一篇文章,所以如果我做错了什么,请耐心等待.

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

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

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.

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

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

InteractiveAuthentication"永远不会被触发,即使客户端启动需要身份验证的请求.

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

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

Anybody has an idea about the property meanings and usage?

非常感谢阿尔贝托

推荐答案

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

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

当需要身份验证时,您的主机是否返回 Unauthorized 状态代码?用户名和密码.他们是否有人居住?

Is your host returning a status code of Unauthorized when authentication is required? What about username & password. Are they populated?

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

The relevant code is here: src/ServiceStack.Common/ServiceClient.Web/ServiceClientBase.cs inside the protected virtual bool HandleResponseException method.

这篇关于ServiceStack JsonServiceClient OnAuthenticationRequired的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆