majlis / Controllers /AuthController.cs
almakhmri253-stack
security hardening: rate limiting, account lockout, security headers, password strength
8a2f194
Raw History Blame Contribute Delete
1.95 kB
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using MajlisManagement.DTOs;
using MajlisManagement.Services.Interfaces;
namespace MajlisManagement.Controllers;
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly IAuthService _auth;
public AuthController(IAuthService auth) => _auth = auth;
/// <summary>تسجيل مستخدم جديد</summary>
[HttpPost("register")]
[EnableRateLimiting("auth")]
public async Task<IActionResult> Register([FromBody] RegisterDto dto)
{
var result = await _auth.RegisterAsync(dto);
return Ok(result);
}
/// <summary>تسجيل الدخول</summary>
[HttpPost("login")]
[EnableRateLimiting("auth")]
public async Task<IActionResult> Login([FromBody] LoginDto dto)
{
var ip = HttpContext.Connection.RemoteIpAddress?.ToString();
var result = await _auth.LoginAsync(dto, ip);
return Ok(result);
}
/// <summary>تغيير كلمة المرور</summary>
[HttpPost("change-password")]
[Authorize]
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordDto dto)
{
var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
await _auth.ChangePasswordAsync(userId, dto);
return Ok(new { message = "تم تغيير كلمة المرور بنجاح" });
}
/// <summary>بيانات المستخدم الحالي</summary>
[HttpGet("me")]
[Authorize]
public IActionResult Me()
{
return Ok(new
{
Id = User.FindFirstValue(ClaimTypes.NameIdentifier),
Name = User.FindFirstValue(ClaimTypes.Name),
Email = User.FindFirstValue(ClaimTypes.Email),
Role = User.FindFirstValue(ClaimTypes.Role)
});
}
}