-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoginUserCommand.cs
More file actions
83 lines (77 loc) · 3.43 KB
/
Copy pathLoginUserCommand.cs
File metadata and controls
83 lines (77 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using MediatR;
using CleanArchitecture.ApiTemplate.Core.Application.Common.Models;
using CleanArchitecture.ApiTemplate.Core.Application.Common.DTOs;
namespace CleanArchitecture.ApiTemplate.Core.Application.Features.Authentication.Commands
{
/// <summary>
/// Command to authenticate a user and generate a JWT token using CQRS pattern.
/// </summary>
/// <remarks>
/// This command implements the CQRS pattern for user authentication and token generation.
/// It provides a clean separation between the request (command) and the business logic (handler).
///
/// Usage:
/// - Called during login operations
/// - Generates JWT token with user claims
/// - Follows existing CQRS patterns in the application
///
/// Security Features:
/// - Username validation
/// - Role-based token generation
/// - Comprehensive error handling
/// - Audit logging through handler
/// - Client context tracking
/// </remarks>
public class LoginUserCommand : IRequest<Result<LoginResponseDto>>
{
/// <summary>
/// Username for authentication
/// </summary>
public string Username { get; }
/// <summary>
/// Password for authentication (validated but not stored in this demo)
/// </summary>
public string Password { get; }
/// <summary>
/// Requested role (User or Admin)
/// </summary>
public string Role { get; }
/// <summary>
/// IP address of the client requesting login (for security logging)
/// </summary>
public string? ClientIpAddress { get; }
/// <summary>
/// User agent of the client (for security logging)
/// </summary>
public string? UserAgent { get; }
/// <summary>
/// Initializes a new instance of the LoginUserCommand.
/// </summary>
/// <param name="username">Username for authentication</param>
/// <param name="password">Password for authentication</param>
/// <param name="role">Requested role (User or Admin)</param>
/// <param name="clientIpAddress">Client IP address for audit logging</param>
/// <summary>
/// Creates a command to authenticate a user and initiate generation of a login response (token and claims).
/// </summary>
/// <param name="username">Username used for authentication (required).</param>
/// <param name="password">Password used for authentication (required).</param>
/// <param name="role">Requested user role; defaults to "User" when not provided.</param>
/// <param name="clientIpAddress">Optional client IP address for audit and logging.</param>
/// <param name="userAgent">Optional client user agent string for audit and logging.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> or <paramref name="password"/> is null.</exception>
public LoginUserCommand(
string username,
string password,
string role = "User",
string? clientIpAddress = null,
string? userAgent = null)
{
Username = username ?? throw new ArgumentNullException(nameof(username));
Password = password ?? throw new ArgumentNullException(nameof(password));
Role = role ?? "User";
ClientIpAddress = clientIpAddress;
UserAgent = userAgent;
}
}
}