Technology

C# MAUI Identity Access Management

c maui identity access management
C# MAUI Identity Access Management | SiamCafe Blog
2026-01-16· อ. บอม — SiamCafe.net· 8,534 คำ

MAUI Identity Access Management

.NET MAUI C# Cross-Platform Identity Access Management OAuth2 RBAC Authentication Authorization Biometric MSAL Token Android iOS Windows macOS

FrameworkLanguagePlatformsUI Approachเหมาะกับ
.NET MAUIC#/XAMLAndroid/iOS/Win/MacNative ControlsEnterprise .NET
FlutterDartAndroid/iOS/Web/DesktopCustom RenderStartup Mobile
React NativeJavaScriptAndroid/iOSNative BridgeWeb Developer
Kotlin MultiplatformKotlinAndroid/iOS/DesktopShared LogicAndroid Developer

MAUI App กับ Authentication

# === .NET MAUI Authentication ===

# dotnet new maui -n SecureApp
# cd SecureApp
# dotnet add package Microsoft.Identity.Client
# dotnet add package CommunityToolkit.Mvvm

# MauiProgram.cs
# using Microsoft.Identity.Client;
#
# public static class MauiProgram
# {
#     public static MauiApp CreateMauiApp()
#     {
#         var builder = MauiApp.CreateBuilder();
#         builder.UseMauiApp();
#
#         // Register MSAL
#         var pca = PublicClientApplicationBuilder
#             .Create("your-client-id")
#             .WithRedirectUri("msauth://callback")
#             .WithAuthority("https://login.microsoftonline.com/common")
#             .Build();
#
#         builder.Services.AddSingleton(pca);
#         builder.Services.AddSingleton();
#         builder.Services.AddTransient();
#
#         return builder.Build();
#     }
# }

# AuthService.cs
# public class AuthService : IAuthService
# {
#     private readonly IPublicClientApplication _pca;
#     private readonly string[] _scopes = { "User.Read", "api://app/access" };
#
#     public async Task LoginAsync()
#     {
#         try
#         {
#             var result = await _pca.AcquireTokenInteractive(_scopes)
#                 .WithParentActivityOrWindow(Platform.CurrentActivity)
#                 .ExecuteAsync();
#             await SecureStorage.SetAsync("access_token", result.AccessToken);
#             return new AuthResult(true, result.Account.Username);
#         }
#         catch (MsalException ex)
#         {
#             return new AuthResult(false, ex.Message);
#         }
#     }
#
#     public async Task GetTokenSilentAsync()
#     {
#         var accounts = await _pca.GetAccountsAsync();
#         var result = await _pca.AcquireTokenSilent(_scopes, accounts.FirstOrDefault())
#             .ExecuteAsync();
#         return result.AccessToken;
#     }
# }

from dataclasses import dataclass
from typing import List

@dataclass
class AuthFlow:
    name: str
    use_case: str
    security: str
    complexity: str

flows = [
    AuthFlow("Authorization Code + PKCE", "Mobile App", "สูงมาก", "ปานกลาง"),
    AuthFlow("Device Code", "TV/IoT", "สูง", "ง่าย"),
    AuthFlow("Client Credentials", "Service-to-Service", "สูง", "ง่าย"),
    AuthFlow("Biometric + Token", "High Security App", "สูงมาก", "สูง"),
]

print("=== OAuth2 Flows for MAUI ===")
for f in flows:
    print(f"  [{f.name}]")
    print(f"    Use: {f.use_case} | Security: {f.security} | Complexity: {f.complexity}")

RBAC และ Authorization

# === Role-Based Access Control ===

# Models/User.cs
# public class AppUser
# {
#     public string Id { get; set; }
#     public string Name { get; set; }
#     public string Email { get; set; }
#     public List Roles { get; set; }
#     public List Permissions { get; set; }
# }

# Services/AuthorizationService.cs
# public class AuthorizationService
# {
#     public bool HasRole(AppUser user, string role)
#         => user.Roles.Contains(role);
#
#     public bool HasPermission(AppUser user, string permission)
#         => user.Permissions.Contains(permission);
#
#     public bool CanAccess(AppUser user, string resource)
#     {
#         return resource switch
#         {
#             "admin-panel" => HasRole(user, "Admin"),
#             "reports" => HasRole(user, "Manager") || HasRole(user, "Admin"),
#             "profile" => true,  // All authenticated users
#             _ => false,
#         };
#     }
# }

# XAML — Conditional UI based on Role
# 

Biometric Authentication

# === Biometric Login ===

# dotnet add package Plugin.Fingerprint

# BiometricService.cs
# using Plugin.Fingerprint;
# using Plugin.Fingerprint.Abstractions;
#
# public class BiometricService
# {
#     public async Task AuthenticateAsync()
#     {
#         var isAvailable = await CrossFingerprint.Current.IsAvailableAsync();
#         if (!isAvailable) return false;
#
#         var request = new AuthenticationRequestConfiguration(
#             "Biometric Login",
#             "ยืนยันตัวตนด้วยลายนิ้วมือหรือ Face ID"
#         );
#
#         var result = await CrossFingerprint.Current.AuthenticateAsync(request);
#         return result.Authenticated;
#     }
# }

# LoginViewModel.cs
# public partial class LoginViewModel : ObservableObject
# {
#     [ObservableProperty] string email;
#     [ObservableProperty] string password;
#     [ObservableProperty] bool isBusy;
#
#     [RelayCommand]
#     async Task BiometricLoginAsync()
#     {
#         IsBusy = true;
#         var biometric = new BiometricService();
#         if (await biometric.AuthenticateAsync())
#         {
#             var token = await SecureStorage.GetAsync("access_token");
#             if (token != null)
#                 await Shell.Current.GoToAsync("//main");
#         }
#         IsBusy = false;
#     }
# }

security_features = {
    "SecureStorage": "เก็บ Token ใน Keychain/Keystore Encrypted",
    "Biometric": "ลายนิ้วมือ Face ID ก่อนเข้าแอป",
    "Certificate Pinning": "ป้องกัน MITM ตรวจ SSL Certificate",
    "App Obfuscation": "ป้องกัน Reverse Engineering",
    "Token Rotation": "Refresh Token อัตโนมัติ ลด Risk",
    "Jailbreak Detection": "ตรวจ Rooted/Jailbroken Device",
    "Secure Communication": "HTTPS TLS 1.3 ทุก API Call",
}

print("Security Features:")
for feature, desc in security_features.items():
    print(f"  [{feature}]: {desc}")

เคล็ดลับ

.NET MAUI คืออะไร

Cross-Platform C# XAML Android iOS Windows macOS Codebase เดียว Hot Reload MVVM Native API Dependency Injection

Identity Access Management คืออะไร

IAM ตัวตน สิทธิ์ Authentication Authorization RBAC OAuth2 OpenID Connect Token MFA Biometric ลายนิ้วมือ Face ID

MAUI กับ Flutter ต่างกันอย่างไร

MAUI C# XAML Native Controls Enterprise .NET Flutter Dart Custom Render Mobile First Community ใหญ่กว่า Performance ดีทั้งสอง

OAuth2 ใน MAUI ใช้อย่างไร

MSAL Authorization Code PKCE Mobile SecureStorage Refresh Token Azure AD Google Apple Biometric Gate

สรุป

.NET MAUI C# Cross-Platform Identity Access Management OAuth2 PKCE MSAL RBAC Biometric SecureStorage MVVM Android iOS Windows macOS Authentication Authorization

📖 บทความที่เกี่ยวข้อง

Go Wire DI Identity Access Managementอ่านบทความ → Go GORM Identity Access Managementอ่านบทความ → WebSocket Scaling Identity Access Managementอ่านบทความ → LLM Fine-tuning LoRA Identity Access Managementอ่านบทความ → Svelte Stores Identity Access Managementอ่านบทความ →

📚 ดูบทความทั้งหมด →