protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) { EFCoreObjectSpaceProvider eFCoreObjectSpaceProvider = new SecuredEFCoreObjectSpaceProvider((ISelectDataSecurityProvider)Security, typeof(EFCoreDemoDbContext), TypesInfo, args.ConnectionString, (builder, connectionString) => { builder.UseSqlServer(connectionString); }); args.ObjectSpaceProviders.Add(eFCoreObjectSpaceProvider); args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null)); }
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(); }
static void Main() { AuthenticationStandard authentication = new AuthenticationStandard(); SecurityStrategyComplex security = new SecurityStrategyComplex( typeof(PermissionPolicyUser), typeof(PermissionPolicyRole), authentication ); string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SecuredEFCoreObjectSpaceProvider objectSpaceProvider = new SecuredEFCoreObjectSpaceProvider(security, typeof(ApplicationDbContext), (builder, _) => builder.UseSqlServer(connectionString)); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MainForm mainForm = new MainForm(security, objectSpaceProvider); Application.Run(mainForm); }
static void Main() { 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) ); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MainForm mainForm = new MainForm(security, objectSpaceProvider); Application.Run(mainForm); }