private void LogoutButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { foreach (Form form in MdiChildren) { form.Close(); } string userName = security.UserName; security.Logoff(); Hide(); ShowLoginForm(userName); }
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(); }