【问题标题】:Getting .NET Client to recognize authentication session cookie让 .NET 客户端识别身份验证会话 cookie
【发布时间】:2014-03-17 23:38:59
【问题描述】:

我正在使用“RememberMe=true”,并希望我的服务客户端重新使用打开的会话(如果可用)。我从下面的链接中获得了大部分代码 - 此代码有效,但每次验证都失败并重新验证。我是否必须以某种方式发送 ss-pid cookie?

还有一点需要注意:这是一个访问我的 servicestack 服务的 WinForms 客户端。

ServiceStack JsonServiceClient OnAuthenticationRequired

我的代码

    Private Shared _UserName As String = "xxxxx"
    Private Shared _Password As String = "yyyyy"

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

    Public Shared ReadOnly Property ServiceClient() As JsonServiceClient
        Get
            If _serviceClient Is Nothing Then
                _serviceClient = New JsonServiceClient(ServiceContext.ServiceUrl)
                _serviceClient.OnAuthenticationRequired = _clientAuthenticationRequested
                _serviceClient.UserName = _UserName
                _serviceClient.Password = _Password

                //service requiring authentication
                Dim v = _serviceClient.Get(Of Tonto.Svc.Model.AppConstants)(
                    New Tonto.Svc.Model.AppConstants())

            End If
            Return _serviceClient
        End Get
    End Property

    Private Shared Sub InteractiveAuthentication(sourcerequest As System.Net.WebRequest)

        Dim v = _serviceClient.Send(Of ServiceStack.AuthenticateResponse)(
            New ServiceStack.Authenticate() With {
                .UserName = _UserName,
                .Password = _Password,
                .RememberMe = True})             

    End Sub

【问题讨论】:

    标签: authentication servicestack


    【解决方案1】:

    您不能让客户端记住您在开箱即用的客户端创建之间的会话。 RememberMe 选项在这里不起作用,因为客户端没有像 Web 浏览器那样的持久 cookie 存储。

    但是,您可以访问客户端的 cookie 存储,在您进行身份验证后读取会话值 cookie,并在未来的客户端实例中恢复它。本质上,您提供了持久层。

    对不起,它是 c# 而不是 VB。但我认为这个概念应该足够清晰。

      var host = "http://localhost:9001";
      JsonServiceClient client = new JsonServiceClient(host);
    
      // Authenticate with the service
      client.Post(new Authenticate { UserName = "test", Password = "password" });
    
      // Read the session cookie after successfully authenticating
      var cookies = client.CookieContainer.GetCookies(new Uri(host));
      var sessionCookieValue = cookies["ss-id"].Value;
    
      // Store the value of sessionCookieValue, so you can restore this session later
    
      client = null; 
    

    因此,如果您要将 ss-id 值保存到文件中,您可以在应用程序启动时恢复该值,然后在发出请求之前将其添加回客户端的 cookie 存储区。

      // Another client instance ... we will reuse the session
      JsonServiceClient anotherClient = new JsonServiceClient(host);
    
      // Restore the cookie
      anotherClient.CookieContainer.Add(new Cookie("ss-id", sessionCookieValue, "/", "localhost"));
    
      // Try access a secure service
      anotherClient.Get(new TestRequest());
    

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 2021-06-28
      • 2019-10-29
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2020-12-16
      • 2020-03-03
      相关资源
      最近更新 更多