expressappframework-403861-backend-web-api-service-create-custom-endpoints-access-object-space-security-and-caption-helper-in-custom-endpoint.md
This topic describes how to access an Object Space, the Security System, and the Caption Helper from custom endpoint methods.
|
Object to access
|
Injecting service
|
Methods and properties
| | --- | --- | --- | |
|
IObjectSpaceFactory
INonSecuredObjectSpaceFactory
|
IObjectSpaceFactory.CreateObjectSpace
INonSecuredObjectSpaceFactory.CreateObjectSpace
| |
|
ISecurityProvider
ISecurityStrategyBase
|
ISecurityProvider.GetSecurity
ISecurityStrategyBase.IsAuthenticated
ISecurityStrategyBase.User
| |
|
|
ICaptionHelperProvider.GetCaptionHelper
|
To create an Object Space in an ASP.NET Core application, inject the IObjectSpaceFactory service. Note that this service ensures if a user is logged on. If not, it throws an authorization exception. To avoid this exception, you can use the following techniques:
Use the INonSecuredObjectSpaceFactory service instead of IObjectSpaceFactory.
Use another way to ensure if a user is logged on (for example, use AuthorizationPolicyBuilder.RequireXafAuthentication with AuthorizeAttribute).
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
namespace MySolution.WebApi {
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class CustomEndpointController : ControllerBase {
IObjectSpaceFactory objectSpaceFactory;
public CustomEndpointController(IObjectSpaceFactory objectSpaceFactory) {
this.objectSpaceFactory = objectSpaceFactory;
}
[HttpGet]
public IActionResult Get() {
using IObjectSpace newObjectSpace = objectSpaceFactory.CreateObjectSpace<Contact>();
// ...
return Ok();
}
//...
}
}
To access the Security System in an ASP.NET Core application, inject the ISecurityProvider service. Note that this service ensures if a user is logged on. If not, it throws an authorization exception. To avoid this exception, you can use the following techniques:
ISecurityStrategyBase service instead of ISecurityProvider if you do not need to operate with an authenticated user object. This service does not ensure if a user is logged on or not, so the current user object might not be available here.AuthorizationPolicyBuilder.RequireXafAuthentication with AuthorizeAttribute.These workarounds do not guarantee that you will not receive authentication exceptions, even if you specify the correct user credentials.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using MySolution.Module.BusinessObjects;
namespace MySolution.WebApi {
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class CustomEndpointController : ControllerBase {
ISecurityProvider securityProvider;
public CustomEndpointController(ISecurityProvider securityProvider) {
this.securityProvider = securityProvider;
}
[HttpGet]
public IActionResult Get() {
ISecurityStrategyBase security = securityProvider.GetSecurity();
var userId = security.UserId;
// ...
return Ok();
}
//...
}
}
The Caption Helper class allows you to get localized captions for XAF Controllers and Razor Components that are shown in XAF Views. XAF initializes the Application Model of this class on requests to standard XAF pages. To get captions with other requests (middleware, Web API method, and so on), use the ICaptionHelperProvider service.
This service uses a shared Application Model and does not return user-specific localized strings from a user Model Differences Storage. If your application does not store different captions for different users in the Application Model, use the ICaptionHelperProvider service as a unified way to get localized captions.
using DevExpress.ExpressApp.AspNetCore.Services.Localization;
using DevExpress.ExpressApp.Utils;
using Microsoft.AspNetCore.Mvc;
namespace MySolutionName.Module.Blazor.Controllers {
public class CustomLocalizationController : ControllerBase {
internal ICaptionHelperProvider captionHelperProvider;
public CustomLocalizationController(ICaptionHelperProvider captionHelperProvider) {
this.captionHelperProvider = captionHelperProvider;
}
[HttpGet]
public string GetActionCaption(string actionName) {
ICaptionHelper helper = captionHelperProvider.GetCaptionHelper();
return helper.GetActionCaption(actionName);
}
}
}
The CaptionHelper.GetService method returns an ICaptionHelper instance to use on other platforms such as Win and Web.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Utils;
ICaptionHelper helper = CaptionHelper.GetService(Application.ServiceProvider)
string newActionName = helper.GetActionCaption("New");
// ...
See Also
Access Object Space, Security System, and Caption Helper in the ASP.NET Core Environment