Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Silverlight/WCF登录会话_C#_.net_Wcf_Silverlight_Security - Fatal编程技术网

C# Silverlight/WCF登录会话

C# Silverlight/WCF登录会话,c#,.net,wcf,silverlight,security,C#,.net,Wcf,Silverlight,Security,我正在使用Silverlight开发一个系统,并使用WCF调用服务来完成服务器端的所有工作 我需要有一个用户登录到系统中,一旦他们被验证,对服务器的所有调用都需要包含用户信息,以便服务器可以检查安全策略并根据用户执行其他操作 最好的方法是什么?我可以创建某种类型的用户类,并在每次调用时将其发送到服务器,但是否有更好的方法使用Silverlight和/或WCF实现这一点 我会使用标准的基于令牌的方法。当您登录到服务器时(按照您的建议通过用户类传递所有必需的信息),服务器将使用令牌进行响应。每隔一次

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

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


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

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


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

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

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

<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
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