无论如何知道哪个用户在服务器端调用WCF Ria服务,在客户端使用silverlight?

时间:2011-12-29 02:53:21

标签: silverlight wcf-ria-services

无论如何知道哪个用户在服务器端调用WCF Ria服务?客户端是siverlight,用户必须首先进行身份验证才能使用该系统。

我需要知道哪个用户实际在我当前的任务中调用该服务,谢谢,我搜索了很多,但似乎没有好的发现。

2 个答案:

答案 0 :(得分:0)

一旦客户端成功清除了您的身份验证质询,服务器就可以向客户端的呼叫者发出令牌。在后续对服务器的调用中,客户端会将令牌作为参数之一发送,服务器将验证令牌并做出相应的响应。

令牌可以包含标识给定用户的一段信息,实现此功能将提供您正在寻找的功能。

生成令牌的唯一指导原则是它们是唯一的,不可预测的和可过期的。我总是加密我的令牌,使它们看起来像胡言乱语,但步骤这是可选的。

答案 1 :(得分:0)

在我得到解决方案之前,我也做了很多“googleing”并且遇到了很多麻烦。 我不使用RIA服务 - 但它应该(希望)相同......:

SL-Client向服务器发送“登录请求”。

在(WCF)服务器端,我执行以下操作(LoginData = SL-Client的Return-Info):

public LoginData LoginRequest() {
    (...)
    OperationContext context = OperationContext.Current;
    System.ServiceModel.Channels.MessageProperties prp = context.IncomingMessageProperties;
    System.ServiceModel.Channels.RemoteEndpointMessageProperty endPrp = prp[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
        (...)
        try {
            clientIP = endPrp.Address;
            System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse(clientIP);
            System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(ipAddress);
        (...)

如果要检查用户WindowsPrincipal,可以执行以下操作(securityGroup =服务器端设置,用户可以登录):

    (...)
    switch (securityGroup) {
            case SecurityGroup.AllClientsCanAccess: {
                clientCanLogin = true;
            } break;
            case SecurityGroup.UseWindowsCredentials: {
                if (OperationContext.Current.ServiceSecurityContext != null && OperationContext.Current.ServiceSecurityContext.WindowsIdentity != null) {
                        if (OperationContext.Current.ServiceSecurityContext.WindowsIdentity.IsAuthenticated) {

                            if (subSecurityInfo1 == true) { // only clients in specific roles can log in
                                bool userRoleFound = false;

                                WindowsPrincipal userPrincipal = new WindowsPrincipal(OperationContext.Current.ServiceSecurityContext.WindowsIdentity);
                                if (userPrincipal == null)
                                    break;

                                foreach (string userRoleToPass in subSecurityList) {    // subSecurityList = settings, which roles can pass

                                    loginError.ErrorInfo += string.Format("{0}\n", userRoleToPass);

                                    if (userPrincipal.IsInRole(userRoleToPass)) {
                                        clientCanLogin = userRoleFound = true;
                                        break;
                                    }
                                }

                                if (userRoleFound) {
                                    loginError.ErrorInfo = string.Empty;
                                    break;
                                }
                                else {
                                    loginError.ErrorNo = LoginErrorCodeNoEnum.UserIsNotInRole;
                                }
                            }
                            (...)

希望它有所帮助...