Example #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";
            try
            {
                if (!Membership.ValidateUser(userName, password))
                {
                    result.Message = "The username or password you entered is incorrect.";
                    return;
                }


                IObjectSpace     objectSpace    = new ODataObjectSpace();
                CriteriaOperator userNameFilter = new BinaryOperator("UserName", userName);
                var user =
                    objectSpace.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      = objectSpace.GetObjects("UserRole", new BinaryOperator("UserId", userId), extraColumns);
                var         currentRoles   = userRoles.AsQueryable().Select("Role").ToArrayList();
                var         userPrivileges = new List <Privilege>();
                List <Guid> roleIds        = new List <Guid>();
                foreach (var roleObject in currentRoles)
                {
                    var role = (Katrin.Domain.Impl.Role)roleObject;
                    if (!roleIds.Contains(role.RoleId))
                    {
                        roleIds.Add(role.RoleId);
                    }
                    else
                    {
                        continue;
                    }
                    objectSpace.LoadProperty(role, "RolePrivileges");

                    var rolePrivileges = role.RolePrivileges;
                    foreach (var rolePrivilege in rolePrivileges)
                    {
                        objectSpace.LoadProperty(rolePrivilege, "Privilege");
                        var privilege = rolePrivilege.Privilege;
                        var name      = (string)privilege.Name;
                        objectSpace.LoadProperty(privilege, "PrivilegeEntities");
                        var privilegeEntities = privilege.PrivilegeEntities;
                        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;
                _loginSuccess = true;
            }
            catch (ThreadAbortException)
            {
                //There just catch the abort exception and then leave this catch block.
            }
            catch (Exception ex)
            {
                //result.Message = BuildExceptionString(ex);
                result.Message = ex.Message;
                MessageService.ShowException(ex);
            }
        }