代码之家  ›  专栏  ›  技术社区  ›  NotDan

Silverlight/WCF登录会话

  •  1
  • NotDan  · 技术社区  · 17 年前

    我正在使用silverlight开发一个系统,并使用wcf调用服务来完成服务器端的所有工作。

    我需要有一个用户登录到系统中,一旦他们被验证,所有对服务器的调用都需要包含用户信息,以便服务器可以检查安全策略并根据用户执行其他操作。

    最好的方法是什么?我可以创建某种用户类,并在每次调用时都将其发送到服务器,但是有没有更好的方法使用silverlight和/或wcf来实现这一点?

    2 回复  |  直到 13 年前
        1
  •  2
  •   David Pokluda    17 年前

    我将使用标准的基于令牌的方法。当您登录到服务器时(按照您的建议通过用户类传递所有必需的信息),服务器将使用令牌进行响应。每一个其他服务器调用都需要一个有效的令牌。然后,服务器验证令牌是否仍然有效(它将在一段时间后自动过期)以及它来自同一台计算机/用户(例如,您可以检查IP地址)。

    这可能是我实现的方式。您不想在每次服务器调用时传递所有用户信息。(如果您在Intranet上,则可能需要使用模拟或类似的方式。)

        2
  •  0
  •   user1512559    13 年前

    Silverlight控件不能直接访问会话变量,因为Silverlight控件是客户端控件。但是我们可以调用WCF服务来管理Silverlight中的会话。

    我们必须在wcf服务中设置会话变量,如下所示。

    <ServiceContract(Namespace:="")> _
    <AspNetCompatibilityRequirements
    (RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
    Public Class PersonService
        <OperationContract()> _
        Public Sub DoWork()
            ' Add your operation implementation here
        End Sub
        ' Add more operations here and mark them with <OperationContract()>
    
       <OperationContract()> _
       Public Sub SetSessionVariable(ByVal Sessionkey As String)
            System.Web.HttpContext.Current.Session("Key") = Sessionkey
            System.Web.HttpContext.Current.Session.Timeout = 20
        End Sub
        <OperationContract()> _
        Public Function GetSessionVariable() As String
            Return System.Web.HttpContext.Current.Session("Key")
        End Function
    
    End Class
    

    通过将服务引用到Silverlight应用程序,我们可以在.xaml页中设置会话变量,如下所示。

    Dim client As Service.PersonServiceClient = New Service.PersonServiceClient()
    'Calls the SetSessionVariable() and store values in the session.
    client.SetSessionVariableAsync("Soumya")
    
    We will get the session variable in the .xaml page by calling GetSessionVariable() where we want to check the session
    
    Dim client As Service.PersonServiceClient = New Service.PersonServiceClient()
    AddHandler client.GetSessionVariableCompleted, AddressOf client_GetSessionVariableCompleted
    client.GetSessionVariableAsync()
    
    Private Sub client_GetSessionVariableCompleted(ByVal sender As Object, ByVal e As GetSessionVariableCompletedEventArgs)
            Try
                If Not String.IsNullOrEmpty(e.Result) Then
                    MessageBox.Show(e.Result)
                Else
                    MessageBox.Show("Your session has been expired")
                End If
            Catch ex As FaultException        
            End Try
    End Sub
    
    推荐文章