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



是否知道哪个用户在服务器端调用WCF Ria服务?客户端是silverlight,用户必须先通过身份验证才能使用系统。

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

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

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

生成令牌的唯一准则是它们是唯一的、不可预测的和可过期的。我总是加密我的令牌,使它们看起来像乱码,但这一步是可选的。

在我找到解决方案之前,我也做了很多"谷歌"和很多头痛。我不使用RIA-Services -但它应该(希望)是相同的…:

SL-Client发送一个"login-request"到服务器。

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

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);
        (...)

如果您想要检查用户windowsprprincipal,您可以执行以下操作(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;
                                }
                            }
                            (...)

最新更新