Security Permissions Caching
Security permission caching is implemented in Security Adapters - classes used by the Security System to process and cache security permission requests. Each Security Adapter has the corresponding Security Adapter Provider class, which is used internally to register the adapter. XPO adapters and their providers are located in the DevExpress.ExpressApp.Security.Xpo.v17.1.dll assembly. The provider and adapter for the Entity Framework are available in the DevExpress.ExpressApp.Security.EF.v17.1.dll assembly. Both these assemblies (XPO and EF) contain the SecurityAdapterHelper class that exposes static methods that enable/disable Security Adapters. So, in your code, you do not need to access Security Adapters directly. This topic describes how to enable and customize caching using the SecurityAdapterHelper class.
Enable Caching in Code
When creating a new XAF project, caching is enabled by the Solution Wizard. The wizard adds the following code line to the WinApplication.cs (WinApplication.vb) and WebApplication.cs (WebApplication.vb) files.
新建项目时,可以在创向导中选择这个选荐.也可以在代码中修改.(winapplication.cs/webapplication.cs)中:
using DevExpress.ExpressApp.Security; // ... SecurityAdapterHelper.Enable();
in the constructor of your platform-agnostic module located in the Module.cs (Module.vb) file;To enable or customize Security permissions caching in an existing application, use the same code, or use another overload of the Enable method that takes an ReloadPermissionStrategy enumeration value. You can call this static method from any place of your code, which is executed before a user is logged on, e.g.:
- in the Main method of the WinForms application located in the Program.cs (Program.vb) file, before the WinApplication.Start call;
- in the Application_Start method of the ASP.NET application located in the Global.asax.cs (Global.asax.vb) file, before the WebApplication.Start call.
The Solution Wizard uses the second and third locations.
Reload Permission Strategies
Security Adapters support various modes of reloading the security permissions. The available modes are listed in the ReloadPermissionStrategy enumeration. There is an overload of the SecurityAdapterHelper.Enable method that takes the reloadPermissionStrategy parameter - use it to change the mode. By default, the NoCache mode is used. Each Session (in XPO) or DBContext (in Entity Framework) operates with the most recent permissions loaded from the database in this mode. You can use the CacheOnFirstAccess mode instead. Permissions are loaded and cached when the corresponding secured data is accessed for the first time, cached permissions are used until the user is logged off in this mode. This mode allows you to significantly reduce the number of database requests when working with secured data.
安全适配器支持各种重新加载安全权限的模式。可用模式在 ReloadPermissionStrategy 枚举中列出。SecurityAdapterHelper 的重载. 启用采用 reloadPermissionStrategy 参数的方法-使用它来更改模式。默认情况下, 使用缓存模式。每个会话 (在 xpo 中) 或 DBContext (在实体框架中) 都使用此模式中从数据库加载的最新权限。您可以改用 CacheOnFirstAccess 模式。当第一次访问相应的安全数据时, 将加载和缓存权限, 直到用户在此模式下注销时才使用缓存的权限。此模式允许您在使用安全数据时显著减少数据库请求的数量。