D365FO X++ authorization: break-glass succeeds but normal user fails—how to trace privilege checks and securityKey/context
We’re seeing a discrepancy in Dynamics 365 Finance & Operations where the same X++ logic succeeds when executed using break-glass/admin, but fails with “insufficient permissions” under the normal user.
The code runs from multiple entry points (1) an interactive form method, (2) a SysOperation/Bacth job, and (3) an OData/service call. In each case the failure occurs during a call to a securable operation (e.g., reading/writing specific records or invoking a table method that triggers record-level checks).
What’s the recommended way to troubleshoot and pinpoint the exact privilege/record-level filter causing the failure?
Specifically:
- How can we capture the security context used by X++ at runtime (user vs batch account vs service caller) and verify what
securityKey()/company/context are affecting authorization? - Are there built-in logs/tools to trace the authorization failure to the missing privilege/operation name (e.g., effective privileges, securable name, record-level access/RecId filtering) for the failing call stack?
- What’s a safe pattern to reproduce the failing scenario locally by forcing the same security context (and, if applicable, using
changeCompany/securityKey/ impersonation-style approaches) so we can determine which privilege is missing?
Environment: D365FO, X++ using SysOperation framework, includes both batch execution and service/OData exposure.
1 answer
Write an answerBelow is a practical D365FO/X++ approach I’ve used to pinpoint where authorization diverges between break-glass/admin and a normal user, especially when the same X++ runs via form, SysOperation/Batch, and OData/service.
1) First capture: “what security context did the runtime actually use?”#
In D365FO, the effective security context depends on entry point (interactive vs batch vs service), and the security key/company that’s active when the securable operation executes.
A. Log the effective values right before the failing call#
Add a structured trace immediately before any call that throws “insufficient permissions” (or before a table read/update that you suspect triggers RLS / record-level security):
public void logAuthContext(str step)
{
// Prefer SysUserId for clarity (works in most runtime paths)
str userId = curUser(); // current user in the executing thread
str currCompany = curext(); // not company; see note below
// curext() is not a good “company” indicator. Use curext() / curext? Avoid.
// In X++, use curext() only if you specifically know your environment; otherwise:
str company = curext(); // NOTE: this is not the typical company function.
// Better: use the DataAreaId on a target buffer, or simply log the caller's company variable.
// Because D365FO authorization depends on DataAreaId/LegalEntity being set.
info(strFmt(
'[AuthTrace] %1 | user=%2 | company=%3 | securityKey=%4 | sessid=%5',
step,
userId,
company,
securityKey(),
sessionId()));
}Why this helps: it tells you whether the batch/service path is still running under the same security key/company as the interactive path, or if it’s running as a different principal (or different company scope).
Edge case:
securityKey()and “current user” can differ across execution types: - Batch commonly runs under batch execution security context (often batch user / service account depending on your config). - OData/Service may run under the authenticated user, but the exact execution principal can still be influenced by how the service is configured.
B. Capture the company the securable operation uses#
Record-level security filters typically include company scope (DataAreaId). So log:
- the
changeCompany()target company (if used) - the
DataAreaIdon the table buffer you’re reading/writing (or the one implied by the method)
If you do something like this:
changeCompany('USMF')
{
// failing call
}log the value of the company you’re switching to and verify the buffer is actually in that company.
C. If you use SysOperation with parm that includes company/user/company#
Ensure your SysOperation contract actually sets the intended company/user context and that you’re not implicitly running on a different company.
2) Pinpoint the securable operation name (and the failing record filter)#
D365FO does not give you a simple “list missing privilege” API call you can rely on in all runtime paths. The most effective approach is:
- catch and rethrow with call-stack + context logs
- enable platform tracing so you can see the security exception details and where it happened.
A. Catch the exception, dump stack + context#
Wrap the suspected call(s):
try
{
logAuthContext('Before failing read/write');
// Example: select/update that triggers RLS/permissions
// select firstOnly forUpdate ...
// ttsbegin; buf.doUpdate(); ttscommit;
}
catch (Exception::CLRError)
{
// framework exceptions sometimes wrap security
// fall through or log
throw;
}
catch (Exception::Error)
{
logAuthContext('After exception (before throw)');
// Call stack is useful for correlating to the exact securable action
// Use Global::getLastError / exception text as well.
info(strFmt('Auth exception: %1', CLRInterop::getAnyTypeForObject(Global::getLastError()).ToString()));
throw;
}Why this helps: the exception message text often isn’t enough to identify the missing privilege/operation, but the stack will let you see:
- which table method / document / data entity method was invoked
- whether the failure happens in read vs write
- where
changeCompanyor contract parameters take effect
If your environment doesn’t show enough in
info(), switch toerror()or a custom logging sink.
B. Use the built-in tracing/diagnostics#
Recommended built-in tools (by category):
- Development Environment trace/diagnostics (to capture call stack and security exception details)
- Application event log / AOS logs (depending on your environment)
- SysTrace / tracing framework is often used internally, but exact availability depends on your tier/support settings.
Because I can’t confirm which exact trace provider your environment has enabled, the most reliable “always works” method is log context + stack around failure, and then use whatever security exception details your tenant emits in the standard logs.
3) Validate effective privileges: do you have the right “operation” type?#
The “same X++ logic succeeds as break-glass but fails for normal user” usually means one of these:
- missing duty/privilege (e.g., “Read/Write” on the securable operation)
- missing record-level security permission (for a given user + record set)
- missing privilege needed only on one entry point (batch/service differs)
- wrong company/legal entity scope
- missing privilege for a specific table/view or a specific method marked as securable
A. Compare the user’s effective security against break-glass#
In the UI / security configuration tools:
- Identify the menu item / duty / role that break-glass uses and compare to the normal user’s roles.
- Look for security policies (record-level security) targeting the table(s) being accessed.
Even without a code API, this is the step that turns “failure at runtime” into “missing permission X.”
4) Reproduce safely the failing scenario with the same context#
Important warning#
In production, you should not attempt “impersonation-style” changes. Also, some context switches will not replicate the exact authorization principal used by service/batch.
A. Reproduce interactively in the normal user context#
The simplest safe reproduction:
- Log into the environment as the normal user.
- Run the same form method / the same controller that your OData/service uses.
- Ensure the same company, record, and parameters.
This usually finds whether the issue is purely privilege/record-level.
B. Reproduce batch/service behavior in a controlled way#
Batch/SysOperation differences are often the root cause.
- If the form call succeeds but batch fails: the batch security principal is different or the contract/company isn’t being set.
- If the service/OData call fails: the security principal is different or company context isn’t carried.
Safe pattern: run two parallel executions with the exact same parameters:
- Batch job triggered as the normal user (if your operational setup supports that)
- Service call authenticated as the normal user
- Compare logged
userId,securityKey(),DataAreaId/company, and recordRecId/keys
C. Verify RecId/record-level filter assumptions#
If record-level security filters by a user-to-record mapping, then selecting by RecId/primary key might still fail if the record is outside the filtered set.
So log the record identity you attempt:
- RecId
- key fields (business key)
- company/legal entity
Example:
info(strFmt('Target record company=%1 RecId=%2', buf.dataAreaId, buf.RecId));If break-glass can access the record but normal user cannot, record-level security is almost certainly involved.
5) Practical “diff checklist” for form vs batch vs service#
When you see discrepancy, compare these items across the three entry points:
- Company/DataAreaId at the moment the securable method runs
- securityKey() (logged)
- curUser()/principal identity (logged)
- Target record identity and whether it differs
- Which securable operation is invoked (read vs update; table method vs direct select)
What I need from you to make this exact#
To give the most concrete “this is the likely missing privilege/operation” guidance, paste:
- the exception text (full message) and whether it mentions table/operation
- the exact call stack frames around the failure
- whether the failing call is a
selector a method update on a specific table - how the service/batch job is invoked (who triggers it; whether you pass company)
With that, I can tell you where to place the logs and which security/record-level filters are most likely involved.
Know the solution?
Sign in to contribute an answer and build your community reputation.
Sign in to answer