Esempio n. 1
0
        private void VarifyUser(UserLoginResult result)
        {
            if (result.Parameter.ServerName == "")
            {
                result.Message = "Please input the server name.";
                return;
            }
            string serverUrl = string.Empty;

            if (result.Parameter.ServiceName != "")
            {
                serverUrl = result.Parameter.ServerName + "/" + result.Parameter.ServiceName;
            }
            else
            {
                serverUrl = result.Parameter.ServerName;
            }
            serverUrl = AppandProtocal(serverUrl);
            Uri serverUri;

            if (!Uri.TryCreate(serverUrl, UriKind.Absolute, out serverUri))
            {
                result.Message = "The server or service you entered is incorrect.";
                return;
            }
            bool isServerReachable = CheckConnection(serverUri);

            if (!isServerReachable)
            {
                result.Message = "The server or service you entered is not available.";
                return;
            }
            string loginPageUrl      = serverUrl + (serverUrl.EndsWith("/") ? "" : "/") + "Login.aspx";
            bool   isLogiPageAvaible = IsUrlReachable(loginPageUrl);

            if (!isLogiPageAvaible)
            {
                result.Message = "The server or service you entered is not available.";
                return;
            }

            UpdateSetting(ServerUrlSettingName, serverUrl);

            LoadMetadata();

            string userName = result.Parameter.UserName;
            string password = result.Parameter.Password;
            var    provider = (ClientFormsAuthenticationMembershipProvider)Membership.Provider;

            provider.ServiceUri = ConfigurationManager.AppSettings["ServerUrl"] + "/Authentication_JSON_AppService.axd";
            if (!Membership.ValidateUser(userName, password))
            {
                result.Message = "The username or password you entered is incorrect.";
                return;
            }
            try
            {
                var dynamicDataServiceContext   = new DynamicDataServiceContext();
                CriteriaOperator userNameFilter = new BinaryOperator("UserName", userName);
                var user =
                    dynamicDataServiceContext.GetObjects("User", userNameFilter, null)._First();

                var userId       = (Guid)user.GetType().GetProperty("UserId").GetValue(user, null);
                var fullName     = (string)user.GetType().GetProperty("FullName").GetValue(user, null);
                var extraColumns = new Dictionary <string, string> {
                    { "Role", "Role" }
                };
                var userRoles      = dynamicDataServiceContext.GetObjects("UserRole", new BinaryOperator("UserId", userId), extraColumns);
                var currentRoles   = userRoles.AsQueryable().Select("Role").ToArrayList();
                var userPrivileges = new List <Privilege>();
                foreach (var role in currentRoles)
                {
                    dynamicDataServiceContext.LoadProperty(role, "RolePrivileges");
                    var rolePrivileges = (IList)role.GetType().GetProperty("RolePrivileges").GetValue(role, null);
                    foreach (var rolePrivilege in rolePrivileges)
                    {
                        dynamicDataServiceContext.LoadProperty(rolePrivilege, "Privilege");
                        var privilege = rolePrivilege.GetType().GetProperty("Privilege").GetValue(rolePrivilege, null);
                        var name      = (string)privilege.GetType().GetProperty("Name").GetValue(privilege, null);
                        dynamicDataServiceContext.LoadProperty(privilege, "PrivilegeEntities");
                        var privilegeEntities =
                            (IList)privilege.GetType().GetProperty("PrivilegeEntities").GetValue(privilege, null);
                        userPrivileges.AddRange(from object privilegeEntity in privilegeEntities
                                                select(string) privilegeEntity.GetType().GetProperty("EntityName")
                                                .GetValue(privilegeEntity, null)
                                                into entityName
                                                select new Privilege()
                        {
                            EntityName = entityName, Name = name
                        });
                    }
                }

                var identity  = new CustomIdentity(userId, userName, fullName);
                var principal = new CustomPrincipal(identity, userPrivileges.ToArray());
                AppDomain.CurrentDomain.SetThreadPrincipal(principal);
                result.Result = true;
            }
            catch (Exception ex)
            {
                result.Message = BuildExceptionString(ex);
            }
        }