Example #1
0
        static void Main()
        {
            XpoTypesInfoHelper.GetXpoTypeInfoSource();
            XafTypesInfo.Instance.RegisterEntity(typeof(Person));
            XafTypesInfo.Instance.RegisterEntity(typeof(PermissionPolicyUser));
            XafTypesInfo.Instance.RegisterEntity(typeof(PermissionPolicyRole));

            Console.WriteLine("Update database...");
            DataSet dataSet = new DataSet();

            Console.WriteLine("Database has been updated successfully.");

            XPObjectSpaceProvider directProvider    = new XPObjectSpaceProvider(new MemoryDataStoreProvider(dataSet));
            IObjectSpace          directObjectSpace = directProvider.CreateObjectSpace();

            UpdateDatabase(directObjectSpace);

            AuthenticationStandard  auth     = new AuthenticationStandard();
            SecurityStrategyComplex security = new SecurityStrategyComplex(typeof(PermissionPolicyUser), typeof(PermissionPolicyRole), auth);

            SecuritySystem.SetInstance(security);
            SecuredObjectSpaceProvider osProvider = new SecuredObjectSpaceProvider(security, new MemoryDataStoreProvider(dataSet));
            IObjectSpace securedObjectSpace       = osProvider.CreateObjectSpace();

            auth.SetLogonParameters(new AuthenticationStandardLogonParameters("User", ""));
            Console.WriteLine("Logging 'User' user...");
            security.Logon(directObjectSpace);
            Console.WriteLine("'User' is logged on.");
            Console.WriteLine("List of the 'Person' objects:");
            foreach (Person person in securedObjectSpace.GetObjects <Person>())
            {
                Console.WriteLine(person.FirstName);
            }

            auth.SetLogonParameters(new AuthenticationStandardLogonParameters("Admin", ""));
            Console.WriteLine("Logging 'Admin' user...");
            security.Logon(directObjectSpace);
            Console.WriteLine("Admin is logged on.");
            securedObjectSpace = osProvider.CreateObjectSpace();
            Console.WriteLine("List of the 'Person' objects:");
            foreach (Person person in securedObjectSpace.GetObjects <Person>())
            {
                Console.WriteLine(person.FirstName);
            }

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            string userName = UserNameBox.Text;
            string password = PasswordBox.Text;
            AuthenticationStandardLogonParameters parameters = new AuthenticationStandardLogonParameters(userName, password);
            SecurityStrategyComplex    security            = ConnectionHelper.GetSecurity(typeof(AuthenticationStandardProvider).Name, parameters);
            SecuredObjectSpaceProvider objectSpaceProvider = ConnectionHelper.GetObjectSpaceProvider(security);
            IObjectSpace logonObjectSpace = objectSpaceProvider.CreateObjectSpace();

            try {
                security.Logon(logonObjectSpace);
            }
            catch { }
            if (security.IsAuthenticated)
            {
                SetCookie(userName);
                FormsAuthentication.RedirectFromLoginPage(userName, true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), null, "errorMessage();", true);
            }
            security.Dispose();
            objectSpaceProvider.Dispose();
        }
        static void LogIn(string login, string password)
        {
            Authentication.SetLogonParameters(new AuthenticationStandardLogonParameters(login, password));
            IObjectSpace loginObjectSpace = ObjectSpaceProvider.CreateObjectSpace();

            Security.Logon(loginObjectSpace);
        }
Example #4
0
        public static SecuredObjectSpaceProvider GetSecuredObjectSpaceProvider(SecurityStrategyComplex security)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SecuredObjectSpaceProvider objectSpaceProvider = new SecuredObjectSpaceProvider(security, connectionString, null);
            IObjectSpace loginObjectSpace = objectSpaceProvider.CreateObjectSpace();

            security.Logon(loginObjectSpace);
            return(objectSpaceProvider);
        }
 private void Login(SecurityStrategyComplex security, IObjectSpaceProvider objectSpaceProvider)
 {
     try
     {
         IObjectSpace objectSpace = objectSpaceProvider.CreateObjectSpace();
         security.Logon(objectSpace);
     }
     catch (Exception ex)
     {
     }
 }
Example #6
0
        protected void Page_Init(object sender, EventArgs e)
        {
            security            = ConnectionHelper.GetSecurity(typeof(IdentityAuthenticationProvider).Name, HttpContext.Current.User.Identity);
            objectSpaceProvider = ConnectionHelper.GetObjectSpaceProvider(security);
            IObjectSpace logonObjectSpace = objectSpaceProvider.CreateObjectSpace();

            security.Logon(logonObjectSpace);
            objectSpace = objectSpaceProvider.CreateObjectSpace();
            EmployeeDataSource.Session   = ((XPObjectSpace)objectSpace).Session;
            DepartmentDataSource.Session = ((XPObjectSpace)objectSpace).Session;
            EmployeeGrid.SettingsText.PopupEditFormCaption = "Employee";
            EmployeeGrid.SettingsPopup.EditForm.Width      = 1000;
        }
        static void Main()
        {
            // ## Step 1. Initialization. Create a Secured Data Store and Set Authentication Options
            PasswordCryptographer.EnableRfc2898       = true;
            PasswordCryptographer.SupportLegacySha512 = false;
            AuthenticationStandard  authentication = new AuthenticationStandard();
            SecurityStrategyComplex security       = new SecurityStrategyComplex(
                typeof(PermissionPolicyUser), typeof(PermissionPolicyRole),
                authentication
                );
            SecuredEFCoreObjectSpaceProvider objectSpaceProvider = new SecuredEFCoreObjectSpaceProvider(
                security, typeof(ApplicationDbContext),
                XafTypesInfo.Instance, ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString,
                (builder, connectionString) => builder.UseSqlServer(connectionString)
                );

            // ## Step 2. Authentication. Log in as a 'User' with an Empty Password
            authentication.SetLogonParameters(new AuthenticationStandardLogonParameters(userName: "******", password: string.Empty));
            IObjectSpace loginObjectSpace = objectSpaceProvider.CreateNonsecuredObjectSpace();

            try {
                security.Logon(loginObjectSpace);
            }
            catch (SqlException sqlEx) {
                if (sqlEx.Number == 4060)
                {
                    throw new Exception(sqlEx.Message + Environment.NewLine + ApplicationDbContext.DatabaseConnectionFailedMessage, sqlEx);
                }
            }

            // ## Step 3. Authorization. Access and Manipulate Data/UI Based on User/Role Rights
            Console.WriteLine($"{"Full Name",-40}{"Department",-40}");
            using (IObjectSpace securedObjectSpace = objectSpaceProvider.CreateObjectSpace()) {
                // User cannot read protected entities like PermissionPolicyRole.
                Debug.Assert(securedObjectSpace.GetObjects <PermissionPolicyRole>().Count == 0);
                foreach (Employee employee in securedObjectSpace.GetObjects <Employee>()) // User can read Employee data.
                // User can read Department data by criteria.
                {
                    bool canRead = security.CanRead(securedObjectSpace, employee, memberName: nameof(Employee.Department));
                    Debug.Assert(!canRead == (employee.Department == null));
                    // Mask protected property values when User has no 'Read' permission.
                    var department = canRead ? employee.Department.Title : "Protected Content";
                    Console.WriteLine($"{employee.FullName,-40}{department,-40}");
                }
            }
            security.Logoff();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
        private void Login_button_Click(object sender, EventArgs e)
        {
            IObjectSpace logonObjectSpace = objectSpaceProvider.CreateObjectSpace();
            string       userName         = userNameEdit.Text;
            string       password         = passwordEdit.Text;

            security.Authentication.SetLogonParameters(new AuthenticationStandardLogonParameters(userName, password));
            try {
                security.Logon(logonObjectSpace);
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #9
0
        static void Main()
        {
            RegisterEntities();
            AuthenticationStandard  authentication = new AuthenticationStandard();
            SecurityStrategyComplex security       = new SecurityStrategyComplex(typeof(PermissionPolicyUser), typeof(PermissionPolicyRole), authentication);

            security.RegisterXPOAdapterProviders();

            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SecuredObjectSpaceProvider objectSpaceProvider = new SecuredObjectSpaceProvider(security, connectionString, null);

            PasswordCryptographer.EnableRfc2898       = true;
            PasswordCryptographer.SupportLegacySha512 = false;

            string userName = "******";
            string password = string.Empty;

            authentication.SetLogonParameters(new AuthenticationStandardLogonParameters(userName, password));
            IObjectSpace loginObjectSpace = objectSpaceProvider.CreateObjectSpace();

            security.Logon(loginObjectSpace);

            using (StreamWriter file = new StreamWriter("result.txt", false)) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append($"{userName} is logged on.\n");
                stringBuilder.Append("List of the 'Employee' objects:\n");
                using (IObjectSpace securedObjectSpace = objectSpaceProvider.CreateObjectSpace()) {
                    foreach (Employee employee in securedObjectSpace.GetObjects <Employee>())
                    {
                        stringBuilder.Append("=========================================\n");
                        stringBuilder.Append($"Full name: {employee.FullName}\n");
                        if (security.IsGranted(new PermissionRequest(securedObjectSpace, typeof(Employee), SecurityOperations.Read, employee, nameof(Department))))
                        {
                            stringBuilder.Append($"Department: {employee.Department.Title}\n");
                        }
                        else
                        {
                            stringBuilder.Append("Department: [Protected content]\n");
                        }
                    }
                }
                file.Write(stringBuilder);
            }
            Console.WriteLine(@"The result.txt file has been created in the \bin\Debug directory.");
            Console.WriteLine("Press any key to close the console...");
            Console.ReadLine();
        }
        private void SetupSecuredObjectSpaceProvider(string logonUserName)
        {
            AuthenticationStandard  authentication = new AuthenticationStandard();
            SecurityStrategyComplex security       = new SecurityStrategyComplex(typeof(UserType), typeof(RoleType), authentication);

            security.RegisterXPOAdapterProviders();
            securedObjectSpaceProvider = CreateSecuredObjectSpaceProvider(security);

            string userName = logonUserName;
            string password = string.Empty;

            authentication.SetLogonParameters(new AuthenticationStandardLogonParameters(userName, password));
            IObjectSpace loginObjectSpace = ((INonsecuredObjectSpaceProvider)securedObjectSpaceProvider).CreateNonsecuredObjectSpace();

            security.Logon(loginObjectSpace);

            SecuritySystem.SetInstance(security);
        }
        static void Main()
        {
            RegisterEntities();
            AuthenticationStandard  authentication = new AuthenticationStandard();
            SecurityStrategyComplex security       = new SecurityStrategyComplex(typeof(PermissionPolicyUser), typeof(PermissionPolicyRole), authentication);

            security.RegisterXPOAdapterProviders();

            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SecuredObjectSpaceProvider objectSpaceProvider = new SecuredObjectSpaceProvider(security, connectionString, null);

            string userName = "******";
            string password = string.Empty;

            authentication.SetLogonParameters(new AuthenticationStandardLogonParameters(userName, password));
            IObjectSpace loginObjectSpace = objectSpaceProvider.CreateObjectSpace();

            security.Logon(loginObjectSpace);

            using (StreamWriter file = new StreamWriter("result.txt", false)) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append($"{userName} is logged on.\n");
                stringBuilder.Append("List of the 'Employee' objects:\n");
                using (IObjectSpace securedObjectSpace = objectSpaceProvider.CreateObjectSpace()) {
                    foreach (Employee employee in securedObjectSpace.GetObjects <Employee>())
                    {
                        stringBuilder.Append("=========================================\n");
                        stringBuilder.Append($"Full name: {employee.FullName}\n");
                        if (security.CanRead(employee, nameof(Department)))
                        {
                            stringBuilder.Append($"Department: {employee.Department.Title}\n");
                        }
                        else
                        {
                            stringBuilder.Append("Department: *******\n");
                        }
                    }
                }
                file.Write(stringBuilder);
            }
            Console.WriteLine(string.Format(@"The result.txt file has been created in the {0} directory.", Environment.CurrentDirectory));
            Console.WriteLine("Press any key to close a the console...");
            Console.ReadLine();
        }
Example #12
0
        private void Login_Click(object sender, EventArgs e)
        {
            IObjectSpace logonObjectSpace = ((INonsecuredObjectSpaceProvider)objectSpaceProvider).CreateNonsecuredObjectSpace();
            string       userName         = userNameEdit.Text;
            string       password         = passwordEdit.Text;

            security.Authentication.SetLogonParameters(new AuthenticationStandardLogonParameters(userName, password));
            try {
                security.Logon(logonObjectSpace);
                DialogResult = DialogResult.OK;
            }
            catch (SqlException sqlEx) {
                if (sqlEx.Number == 4060)
                {
                    XtraMessageBox.Show(sqlEx.Message + Environment.NewLine + ApplicationDbContext.DatabaseConnectionFailedMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) {
                XtraMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Login(SecurityStrategyComplex security, IObjectSpaceProvider objectSpaceProvider)
        {
            IObjectSpace objectSpace = objectSpaceProvider.CreateObjectSpace();

            security.Logon(objectSpace);
        }