🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
SysSingleton and SysUser Model Pattern
This page was created by Wikiadmin on 2026-07-29. Last edited by Wikiadmin on 2026-07-29.

You can use the SysSingleton and SysUser model pattern in a Turnkey app to make the logged-in user available to your model and control access to ViewModels and actions.

What the pattern contains

The SysUserAuthentication pattern integrates user registration and sign-in into a Turnkey app. It uses ASP.NET Core Identity for authentication, which establishes who the user is, and authorization, which establishes what the user is permitted to do.

The pattern adds these model classes:

Class Purpose Example
SysSingleton The application root object for login-related information. It has one instance and exposes the current user to the model. SysSingleton.oclSingleton.CurrentUser.IsAdmin checks whether the current user is an administrator.
SysUser Represents a registered user. A new registration creates a SysUser with a unique UserName.
SysExternalLogin Represents a third-party login. Use it when a user signs in through Google, Microsoft, Facebook, or OpenID.
SysRole Represents a user role that you can use with access groups. Assign permissions for different user groups, such as administrators and standard users.

A SysSession class is also available with the SysUser and SysSingleton pattern.

Use SysSingleton to reach the current user

A singleton is a class with exactly one object instance. Set the Eco.IsSingleton tagged value to true on a singleton class. In MDriven Designer, this is shown as the IsSingleton property in the Objects Inspector.

Use the oclSingleton OCL operator to access that instance. If no instance exists when the expression runs, oclSingleton creates one.

SysSingleton.oclSingleton.CurrentUser.IsAdmin

In this example, CurrentUser is the derived association from SysSingleton to the logged-in SysUser. The expression returns whether the current user has administrator permission.

The CurrentUser association is transient, meaning it is not persisted. This is required because the current user is specific to the active user session.

Add the pattern to a model

  1. Obtain the SysUserAuthentication model example.
  2. Open your model in MDriven Designer.
  3. Use Open Merge Add to merge the downloaded model into your model. See Model Examples and Merge for merge behavior.
  4. Verify that SysSingleton has IsSingleton enabled.
  5. Verify that SysSuperClass is the default superclass for the authentication package.
  6. Start Turnkey and register a user with an email address and password.
  7. In the OCL debugger, evaluate SysUser.allinstances to verify that registration created a SysUser object.

The SysUser class includes UserName, PasswordHash, SecurityStamp, isAdmin, and SendMeNews. Use SetPassword(pwd) to set a password; it creates the password hash by using HashPassword(pwd).

Apply access rules

Use AccessGroups when several ViewModels or actions share the same authorization rule. An AccessGroup expression evaluates to true or false and controls whether its assigned ViewModels and actions can be viewed, are visible, or are enabled.

For example, create an IsLoggedIn AccessGroup and assign it to the CarSeeker ViewModel. A user who is not logged in cannot see CarSeeker; a logged-in user can see it.

For administrator-only functions, use an expression based on the current user:

SysSingleton.oclSingleton.CurrentUser.IsAdmin=true

You can use this expression in ViewModel and action Enable or Visible expressions, or as the basis of an administrator AccessGroup.

Optional per-session properties

Turnkey fills the following optional properties on SysSingleton when they exist. Define them as transient properties so their values remain specific to the user session.

Property Type Value supplied by Turnkey
ClientIp String? Client IP address
SystemType String? TurnkeyNetFramework or TurnkeyNetCore
UserAgent String? User-Agent value from the client

If SysSingleton defines OnCurrentUserChanged, Turnkey calls it whenever CurrentUser is set or cleared, such as when a user logs in or logs out. In MVC, this also occurs on postback.

See also