A lightweight Delphi library for retrieving Windows user authentication information without requiring credentials. The library provides a reliable way to get the currently logged-in Windows user's details with automatic fallback mechanisms.
- Multiple Authentication Methods: Uses Windows Security API with automatic fallback to Network API
- Zero User Interaction: Retrieves user information without prompting for credentials
- Flexible Fallback Modes: Choose between strict authentication or always returning a result
- Clean Interface Design: Simple, intuitive API with proper interface-based architecture
- Cross-Platform Support: Works with both Win32 and Win64 applications
- No External Dependencies: Uses only Windows system DLLs (secur32.dll, netapi32.dll)
- Clone this repository or download the source files
- Add the SSO units to your Delphi project
- Ensure your project targets Windows (Win32 or Win64)
The easiest way to get the current Windows user:
uses
SSO.SingleSignOn;
procedure GetCurrentUser;
begin
TSimpleSingleSignOn.CheckUser(
procedure(UserInfo: IWindowsUserInfo)
begin
if UserInfo.UserFound then
ShowMessage('Hello, ' + UserInfo.FullQualifiedUserName)
else
ShowMessage('Could not determine Windows user');
end);
end;For more control over the authentication process:
uses
SSO.Interfaces,
SSO.Windows;
procedure GetUserWithFallback;
var
SSO: ISSOComposite;
UserInfo: IWindowsUserInfo;
begin
SSO := TSSOWindows.Create;
// Choose fallback mode
SSO.SetFallbackMode(TFallbackMode.AlwaysReturnSomething);
// Get user info
UserInfo := SSO.GetUserInfo;
if UserInfo.UserFound then
begin
WriteLn('Username: ', UserInfo.Username);
WriteLn('Domain: ', UserInfo.Domain);
WriteLn('UPN: ', UserInfo.FullQualifiedUserName);
end;
end;Contains the authenticated user's information:
Username: string- The user's login nameDomain: string- The Windows domain nameFullQualifiedUserName: string- Full UPN (user@domain or domain\user format)UserFound: Boolean- Indicates if user information was successfully retrieved
Main interface for authentication:
SetFallbackMode(Mode: TFallbackMode)- Configure fallback behaviorGetUPN: string- Get user principal name (raises exception if not found)GetUserInfo: IWindowsUserInfo- Get complete user information
TFallbackMode.Minimal- Throws exception if primary methods fail (default)TFallbackMode.AlwaysReturnSomething- Falls back to environment variables if needed
The library attempts authentication in the following order:
-
Windows Security API (Secur32.dll)
- Primary method using
GetUserNameEx - Most reliable for domain-joined computers
- Primary method using
-
Windows Network API (NetAPI32.dll)
- Fallback method using
NetWkstaUserGetInfo - Works when Security API is unavailable
- Fallback method using
-
Environment Variables (optional)
- Only used when
AlwaysReturnSomethingmode is enabled - Reads from
USERNAMEandUSERDOMAINenvironment variables
- Only used when
A complete example application is included in the Examples folder. To run it:
- Open
Examples/SingleSignOnExample.dprojin Delphi - Compile and run the application
- Click "Get User Info" to see the current user's details
- Delphi/RAD Studio (tested with recent versions)
- Windows operating system (Windows 7 or later)
- Target platform: Win32 or Win64
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
For issues, questions, or contributions, please use the GitHub issue tracker.
This library is MIT licensed and free to use. For companies that depend on it commercially we offer support and maintenance agreements with guaranteed response times, and sponsored development of features you need. Contact us at gdksoftware.com/contact-us or open an issue to get in touch.